home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / hex2tx.zip / HEX2TEXT.C next >
C/C++ Source or Header  |  1993-05-03  |  10KB  |  295 lines

  1. /***************************************************************************/
  2. /*Hex2text - Text Formatter for IBM(TM) Telnet Hexdump Output              */
  3. /*-------------------------------------------------------------------      */
  4. /*                                                                         */
  5. /* DISCLAIMER: The author assumes no responsibility for the performance    */
  6. /* or lack thereof of this software & is not responsible for any damage to */
  7. /* data or equipment which might be caused for its use. The user assumes   */
  8. /* all responsibility and risk for use and dermination of suitability of   */
  9. /* this program for any purpose. USE AT YOUR OWN RISK. I do.               */
  10. /*                                                                         */
  11. /* LICENSE: You are free to use this program and modify and distribute it  */
  12. /* to your hearts content provided that this source is distributed with    */
  13. /* the program, and no charge (other than a nominal charge for copying and */
  14. /* distribution, e.g. user groups diskettes) is made.                      */
  15. /*                                                                         */
  16. /* Rationale:                                                              */
  17. /* ----------                                                              */
  18. /*Faced with a need to get flat ASCII text output (in a TTY) fashion from  */
  19. /*the library database server, after installing 1.2.1 TCP/IP for OS/2 2.0, */
  20. /*I found I was not able to coax telnet into anything more than civil than */
  21. /*a 3 field dump containing some input/output line codes, hexadecimal, and */
  22. /*a partial ASCII listing in the rightmost column.                         */
  23. /*                                                                         */
  24. /*The "prettydump" option described in the documentation did not seem to   */
  25. /*affect the format of the output, either on or off. File output from      */
  26. /*telnet is enabled by the -n <tracefile> argument or escaping to the      */
  27. /*telnet command prompt & issuing "set tracefile <logfile>". Capture of    */
  28. /*data is toggled by escaping to the telnet command prompt and setting the */
  29. /*netdata option (e.g. "set netdata on").                                  */
  30. /*                                                                         */
  31. /*What comes out of telnet is like this:                                   */
  32. /*                                                                         */
  33. /*--------------------Start Example Hex Dump-------------------------      */
  34. /*< 0x0                53656172 63682072 6573756c 743a2020  |Search result:  |*/
  35. /*< 0x10        32302063 69746174 696f6e73 20696e20  |20 citations in |*/
  36. /*< 0x20        74686520 4d65646c 696e6520 64617461  |the Medline data|*/
  37. /*< 0x30        62617365 0d0a0d0a 546f2064 6973706c  |base....To displ|*/
  38. /*< 0x40        61792041 42535452 4143542c 20747970  |ay ABSTRACT, typ|*/
  39. /*< 0x50        65204420 3c726563 6f726420 6e756d62  |e D <record numb|*/
  40. /*< 0x60        65723e20 4142532e 0d0a5479 70652048  |er> ABS...Type H|*/
  41. /*< 0x70        454c5020 666f7220 6f746865 72206469  |ELP for other di|*/
  42. /*< 0x80        73706c61 79206f70 74696f6e 732e0d0a  |splay options...|*/
  43. /*--------------------End Example Hex Dump---------------------------      */
  44. /*                                                                         */
  45. /*and what hex2text processes it to is this:                               */
  46. /*                                                                         */
  47. /*----------------------Start Example Conversion---------------------      */
  48. /*Search result:  20 citations in the Medline database                     */
  49. /*                                                                         */
  50. /*To display ABSTRACT, type D <record number> ABS.                         */
  51. /*Type HELP for other display options.                                     */
  52. /*----------------------END Example Conversion-----------------------      */
  53. /*                                                                         */
  54. /*For command line switch options just run hex2text with no arguments.     */
  55. /*                                                                         */
  56. /* Comments and Suggestions (be nice, it's free) to David J. Looney        */
  57. /* Compuserve ID 75246,3364                                                */
  58. /***************************************************************************/
  59.  
  60.     /**** Dependencies/Includes ************/
  61.  
  62. #ifndef _STDIO_H
  63. #include <stdio.h>
  64. #endif
  65. #ifndef _STDLIB_H
  66. #include <stdlib.h>
  67. #endif
  68. #ifndef _STRING_H
  69. #include <string.h>
  70. #endif
  71. #ifndef _STDARG_H
  72. #include <stdarg.h>
  73. #endif
  74. #ifndef _CTYPE_H
  75. #include <ctype.h>
  76. #endif
  77.  
  78.     /**** Function Declarations *************/
  79.  
  80. int toupper(int c);
  81. int hc(int c);
  82. void PrintUsage(int c);
  83.  
  84.     /**** Main Program *********************/
  85.  
  86. void main(int argc, char* argv[])
  87. {
  88.     int  i,j,c,c1,cnt,texln;
  89.     char currntln[256];
  90.     static char *texbuf[12000];
  91.     int StripAnsi, CRetLFeed, KeepInput, KeepOutput, FormatPara;
  92.     FILE *in,*out;
  93.  
  94.     /**** Set Default Settings **************/
  95.  
  96.     StripAnsi= 1;    CRetLFeed= 0;    KeepInput= 1;
  97.     KeepOutput=1;    FormatPara=0;
  98.  
  99.     /**** Exit if Insufficient Arguments ****/
  100.     if(argc<3) PrintUsage(1);
  101.  
  102.     /**** Otherwise Open Files **************/
  103.  
  104.     if((in =fopen(argv[1],"r"))==NULL) PrintUsage(2);
  105.  
  106.     if((out=fopen(argv[2],"w"))==NULL) PrintUsage(3);
  107.  
  108.     /**** Process Options *******************/
  109.  
  110.     for(c=3; c<argc; c++) {
  111.         switch(toupper(argv[c][1])){
  112.             case 'a' :
  113.             case 'A' : StripAnsi=0;
  114.                    break;
  115.             case 'c' :
  116.             case 'C' : CRetLFeed=1;
  117.                    break;
  118.             case 'i' :
  119.             case 'I' : KeepInput=0;
  120.                    break;
  121.             case 'o' :
  122.             case 'O' : KeepOutput=0;
  123.                    break;
  124.             case 'p' :
  125.             case 'P' : FormatPara=1;
  126.                    CRetLFeed=1;
  127.                    break;
  128.             case '?' :
  129.             case 'h' :
  130.             case 'H' :
  131.             default  : PrintUsage(0);
  132.         }
  133.     }
  134.  
  135.     /**** Read, Parse, and Convert Lines ****/
  136.     /**** Read Lines into Buffer ************/
  137.     /**** Discard All but Hex Code **********/
  138.  
  139.     texln=0;
  140.     while(fgets(currntln,256,in)!=NULL){
  141.         if(KeepOutput || currntln[0]=='<') {
  142.          if(KeepInput || currntln[0]=='>') {
  143.         texbuf[texln]=(char*) malloc(strlen(currntln)*sizeof(char));
  144.         if(texbuf[texln]==NULL){
  145.             fprintf(stderr,"Out of Memory !\n");
  146.             exit(4);
  147.         }
  148.         strcpy(texbuf[texln],strchr(currntln,'\t'));
  149.         cnt=0;
  150.         for(c=0; c<40; c++){
  151.             if(!isspace(texbuf[texln][c])){
  152.                 texbuf[texln][cnt]=texbuf[texln][c];
  153.                 cnt++;
  154.             }
  155.         }
  156.         texbuf[texln][cnt]=0;
  157.         texln++;
  158.          }
  159.         }
  160.     }
  161.     fclose(in);
  162.  
  163.     /**** Convert Hex to Binary  ************/
  164.  
  165.     for(i=0; i<texln; i++){
  166.        cnt=0;
  167.        for(j=0; j<strlen(texbuf[i]); j++){
  168.           c=16*hc(texbuf[i][j])+hc(texbuf[i][j+1]);
  169.           j++;
  170.           texbuf[i][cnt]=c;
  171.           cnt++;
  172.        }
  173.        texbuf[i][cnt]=0;
  174.     }
  175.  
  176.     /**** Parse Binary as Indicated & Write */
  177.  
  178.     for(i=0; i<texln; i++){
  179.       cnt=0;
  180.       for(c=0; c<strlen(texbuf[i]); c++){
  181.          if(StripAnsi && texbuf[i][c]==27) c=c+3;
  182.          else {
  183.           if(texbuf[i][c]==0x0C){texbuf[i][cnt]=0x0C; cnt++;}
  184.           if(texbuf[i][c]==0x0D){
  185.             if(CRetLFeed){ texbuf[i][cnt]=0x0A; cnt++;}
  186.             else {texbuf[i][cnt]=0x0D; cnt++;}
  187.           }
  188.           if(texbuf[i][c]==0x0A){
  189.             if(!CRetLFeed){texbuf[i][cnt]=0x0A; cnt++;}
  190.           }
  191.           if(texbuf[i][c]>31 && texbuf[i][c]<127){
  192.             texbuf[i][cnt]=texbuf[i][c];
  193.             cnt++;
  194.           }
  195.          }
  196.       }
  197.       texbuf[i][cnt]=0;
  198.     }
  199.  
  200.     /**** Second Pass for Paragraph Format **/
  201.     if(FormatPara){
  202.        for(i=0; i<texln; i++){
  203.           cnt=0;
  204.           for(c=0; c<strlen(texbuf[i]); c++){
  205.          if(texbuf[i][c]==0x0A){
  206.             if(texbuf[i][c+1]==0x0A){
  207.                c++;
  208.                texbuf[i][cnt]=0x0D;
  209.                cnt++;
  210.                texbuf[i][cnt]=0x0A;
  211.                cnt++;
  212.             }
  213.          }
  214.          else {texbuf[i][cnt]=texbuf[i][c];  cnt++;}
  215.           }
  216.           texbuf[i][cnt]=0;
  217.        }
  218.     }
  219.  
  220.     /**** Output to File, Free Memory *******/
  221.     for(i=0; i<texln; i++){
  222.        for(c=0; c<strlen(texbuf[i]); c++){
  223.           fputc((int) texbuf[i][c],out);
  224.        }
  225.        free((char*) texbuf[i]);
  226.     }
  227.     free((char**)texbuf);
  228.     fclose(out);
  229. }
  230.  
  231.  
  232. int toupper(int c)
  233. {
  234.     return(((c>96)&&(c<123))?c-32:c);
  235. }
  236.  
  237. void PrintUsage(int code)
  238. {
  239.     fprintf(stderr,"╔══════════════════════════════════════════════════════════╗\n");
  240.     fprintf(stderr,"║HEX2TEXT Text Formatter Utility for IBM(TM) Telnet Hexdump║\n");
  241.     fprintf(stderr,"║by David J. Looney, ver. 0.5ß, 05-03-93.                  ║\n");
  242.     fprintf(stderr,"╚══════════════════════════════════════════════════════════╝\n");
  243.     fprintf(stderr,"Usage:\n");
  244.     fprintf(stderr,"hex2text <infile> <outfile> [-/aA] [-/iI] [-/oO] [-/pP] [-/hH?]\n");
  245.     fprintf(stderr,"Where - or / is the delimiter character,\n");
  246.     fprintf(stderr,"  aA|NSI/VTxxx codes are kept,\n");
  247.     fprintf(stderr,"  iI|ncoming lines are skipped,\n");
  248.     fprintf(stderr,"  oO|output from keyboard is skipped,\n");
  249.     fprintf(stderr,"  cC|arraige Return Linefeed toggle,\n");
  250.     fprintf(stderr,"  pP|aragraph keeps CR/LF only if double,\n");
  251.     fprintf(stderr," ?hH|elp causes this message to be printed.\n");
  252.     fprintf(stderr,"E.g. hex2text foo.log foo.txt /o /c works well for EMACS\n\n");
  253.     exit(code);
  254. }
  255.  
  256. int hc(int i)
  257. {
  258.     switch(toupper(i)) {
  259.         case '0'    : i=0;
  260.                   break;
  261.         case '1'    : i=1;
  262.                   break;
  263.         case '2'    : i=2;
  264.                   break;
  265.         case '3'    : i=3;
  266.                   break;
  267.         case '4'    : i=4;
  268.                   break;
  269.         case '5'    : i=5;
  270.                   break;
  271.         case '6'    : i=6;
  272.                   break;
  273.         case '7'    : i=7;
  274.                   break;
  275.         case '8'    : i=8;
  276.                   break;
  277.         case '9'    : i=9;
  278.                   break;
  279.         case 'A'    : i=10;
  280.                   break;
  281.         case 'B'    : i=11;
  282.                   break;
  283.         case 'C'    : i=12;
  284.                   break;
  285.         case 'D'    : i=13;
  286.                   break;
  287.         case 'E'    : i=14;
  288.                   break;
  289.         case 'F'    : i=15;
  290.                   break;
  291.         default        : i=10;
  292.     }
  293.     return(i);
  294. }
  295.