home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
hex2tx.zip
/
HEX2TEXT.C
next >
Wrap
C/C++ Source or Header
|
1993-05-03
|
10KB
|
295 lines
/***************************************************************************/
/*Hex2text - Text Formatter for IBM(TM) Telnet Hexdump Output */
/*------------------------------------------------------------------- */
/* */
/* DISCLAIMER: The author assumes no responsibility for the performance */
/* or lack thereof of this software & is not responsible for any damage to */
/* data or equipment which might be caused for its use. The user assumes */
/* all responsibility and risk for use and dermination of suitability of */
/* this program for any purpose. USE AT YOUR OWN RISK. I do. */
/* */
/* LICENSE: You are free to use this program and modify and distribute it */
/* to your hearts content provided that this source is distributed with */
/* the program, and no charge (other than a nominal charge for copying and */
/* distribution, e.g. user groups diskettes) is made. */
/* */
/* Rationale: */
/* ---------- */
/*Faced with a need to get flat ASCII text output (in a TTY) fashion from */
/*the library database server, after installing 1.2.1 TCP/IP for OS/2 2.0, */
/*I found I was not able to coax telnet into anything more than civil than */
/*a 3 field dump containing some input/output line codes, hexadecimal, and */
/*a partial ASCII listing in the rightmost column. */
/* */
/*The "prettydump" option described in the documentation did not seem to */
/*affect the format of the output, either on or off. File output from */
/*telnet is enabled by the -n <tracefile> argument or escaping to the */
/*telnet command prompt & issuing "set tracefile <logfile>". Capture of */
/*data is toggled by escaping to the telnet command prompt and setting the */
/*netdata option (e.g. "set netdata on"). */
/* */
/*What comes out of telnet is like this: */
/* */
/*--------------------Start Example Hex Dump------------------------- */
/*< 0x0 53656172 63682072 6573756c 743a2020 |Search result: |*/
/*< 0x10 32302063 69746174 696f6e73 20696e20 |20 citations in |*/
/*< 0x20 74686520 4d65646c 696e6520 64617461 |the Medline data|*/
/*< 0x30 62617365 0d0a0d0a 546f2064 6973706c |base....To displ|*/
/*< 0x40 61792041 42535452 4143542c 20747970 |ay ABSTRACT, typ|*/
/*< 0x50 65204420 3c726563 6f726420 6e756d62 |e D <record numb|*/
/*< 0x60 65723e20 4142532e 0d0a5479 70652048 |er> ABS...Type H|*/
/*< 0x70 454c5020 666f7220 6f746865 72206469 |ELP for other di|*/
/*< 0x80 73706c61 79206f70 74696f6e 732e0d0a |splay options...|*/
/*--------------------End Example Hex Dump--------------------------- */
/* */
/*and what hex2text processes it to is this: */
/* */
/*----------------------Start Example Conversion--------------------- */
/*Search result: 20 citations in the Medline database */
/* */
/*To display ABSTRACT, type D <record number> ABS. */
/*Type HELP for other display options. */
/*----------------------END Example Conversion----------------------- */
/* */
/*For command line switch options just run hex2text with no arguments. */
/* */
/* Comments and Suggestions (be nice, it's free) to David J. Looney */
/* Compuserve ID 75246,3364 */
/***************************************************************************/
/**** Dependencies/Includes ************/
#ifndef _STDIO_H
#include <stdio.h>
#endif
#ifndef _STDLIB_H
#include <stdlib.h>
#endif
#ifndef _STRING_H
#include <string.h>
#endif
#ifndef _STDARG_H
#include <stdarg.h>
#endif
#ifndef _CTYPE_H
#include <ctype.h>
#endif
/**** Function Declarations *************/
int toupper(int c);
int hc(int c);
void PrintUsage(int c);
/**** Main Program *********************/
void main(int argc, char* argv[])
{
int i,j,c,c1,cnt,texln;
char currntln[256];
static char *texbuf[12000];
int StripAnsi, CRetLFeed, KeepInput, KeepOutput, FormatPara;
FILE *in,*out;
/**** Set Default Settings **************/
StripAnsi= 1; CRetLFeed= 0; KeepInput= 1;
KeepOutput=1; FormatPara=0;
/**** Exit if Insufficient Arguments ****/
if(argc<3) PrintUsage(1);
/**** Otherwise Open Files **************/
if((in =fopen(argv[1],"r"))==NULL) PrintUsage(2);
if((out=fopen(argv[2],"w"))==NULL) PrintUsage(3);
/**** Process Options *******************/
for(c=3; c<argc; c++) {
switch(toupper(argv[c][1])){
case 'a' :
case 'A' : StripAnsi=0;
break;
case 'c' :
case 'C' : CRetLFeed=1;
break;
case 'i' :
case 'I' : KeepInput=0;
break;
case 'o' :
case 'O' : KeepOutput=0;
break;
case 'p' :
case 'P' : FormatPara=1;
CRetLFeed=1;
break;
case '?' :
case 'h' :
case 'H' :
default : PrintUsage(0);
}
}
/**** Read, Parse, and Convert Lines ****/
/**** Read Lines into Buffer ************/
/**** Discard All but Hex Code **********/
texln=0;
while(fgets(currntln,256,in)!=NULL){
if(KeepOutput || currntln[0]=='<') {
if(KeepInput || currntln[0]=='>') {
texbuf[texln]=(char*) malloc(strlen(currntln)*sizeof(char));
if(texbuf[texln]==NULL){
fprintf(stderr,"Out of Memory !\n");
exit(4);
}
strcpy(texbuf[texln],strchr(currntln,'\t'));
cnt=0;
for(c=0; c<40; c++){
if(!isspace(texbuf[texln][c])){
texbuf[texln][cnt]=texbuf[texln][c];
cnt++;
}
}
texbuf[texln][cnt]=0;
texln++;
}
}
}
fclose(in);
/**** Convert Hex to Binary ************/
for(i=0; i<texln; i++){
cnt=0;
for(j=0; j<strlen(texbuf[i]); j++){
c=16*hc(texbuf[i][j])+hc(texbuf[i][j+1]);
j++;
texbuf[i][cnt]=c;
cnt++;
}
texbuf[i][cnt]=0;
}
/**** Parse Binary as Indicated & Write */
for(i=0; i<texln; i++){
cnt=0;
for(c=0; c<strlen(texbuf[i]); c++){
if(StripAnsi && texbuf[i][c]==27) c=c+3;
else {
if(texbuf[i][c]==0x0C){texbuf[i][cnt]=0x0C; cnt++;}
if(texbuf[i][c]==0x0D){
if(CRetLFeed){ texbuf[i][cnt]=0x0A; cnt++;}
else {texbuf[i][cnt]=0x0D; cnt++;}
}
if(texbuf[i][c]==0x0A){
if(!CRetLFeed){texbuf[i][cnt]=0x0A; cnt++;}
}
if(texbuf[i][c]>31 && texbuf[i][c]<127){
texbuf[i][cnt]=texbuf[i][c];
cnt++;
}
}
}
texbuf[i][cnt]=0;
}
/**** Second Pass for Paragraph Format **/
if(FormatPara){
for(i=0; i<texln; i++){
cnt=0;
for(c=0; c<strlen(texbuf[i]); c++){
if(texbuf[i][c]==0x0A){
if(texbuf[i][c+1]==0x0A){
c++;
texbuf[i][cnt]=0x0D;
cnt++;
texbuf[i][cnt]=0x0A;
cnt++;
}
}
else {texbuf[i][cnt]=texbuf[i][c]; cnt++;}
}
texbuf[i][cnt]=0;
}
}
/**** Output to File, Free Memory *******/
for(i=0; i<texln; i++){
for(c=0; c<strlen(texbuf[i]); c++){
fputc((int) texbuf[i][c],out);
}
free((char*) texbuf[i]);
}
free((char**)texbuf);
fclose(out);
}
int toupper(int c)
{
return(((c>96)&&(c<123))?c-32:c);
}
void PrintUsage(int code)
{
fprintf(stderr,"╔══════════════════════════════════════════════════════════╗\n");
fprintf(stderr,"║HEX2TEXT Text Formatter Utility for IBM(TM) Telnet Hexdump║\n");
fprintf(stderr,"║by David J. Looney, ver. 0.5ß, 05-03-93. ║\n");
fprintf(stderr,"╚══════════════════════════════════════════════════════════╝\n");
fprintf(stderr,"Usage:\n");
fprintf(stderr,"hex2text <infile> <outfile> [-/aA] [-/iI] [-/oO] [-/pP] [-/hH?]\n");
fprintf(stderr,"Where - or / is the delimiter character,\n");
fprintf(stderr," aA|NSI/VTxxx codes are kept,\n");
fprintf(stderr," iI|ncoming lines are skipped,\n");
fprintf(stderr," oO|output from keyboard is skipped,\n");
fprintf(stderr," cC|arraige Return Linefeed toggle,\n");
fprintf(stderr," pP|aragraph keeps CR/LF only if double,\n");
fprintf(stderr," ?hH|elp causes this message to be printed.\n");
fprintf(stderr,"E.g. hex2text foo.log foo.txt /o /c works well for EMACS\n\n");
exit(code);
}
int hc(int i)
{
switch(toupper(i)) {
case '0' : i=0;
break;
case '1' : i=1;
break;
case '2' : i=2;
break;
case '3' : i=3;
break;
case '4' : i=4;
break;
case '5' : i=5;
break;
case '6' : i=6;
break;
case '7' : i=7;
break;
case '8' : i=8;
break;
case '9' : i=9;
break;
case 'A' : i=10;
break;
case 'B' : i=11;
break;
case 'C' : i=12;
break;
case 'D' : i=13;
break;
case 'E' : i=14;
break;
case 'F' : i=15;
break;
default : i=10;
}
return(i);
}