}PHPDESK}/* A Program to Convert HTML files to Wordstar format. Andrew D. Todd v. 3, Jan 31, 1999 ------------------------------------------------------ Syntax is HTM_WS src dst where src, dst are filespecs ---------------------------------------------------------------- environment: MS-DOS, using MIX Power C compiler (superset of Microsoft C, Borland Turbo C), however, should be extremely portable. ----------------------------------------------------------------- This program is in the public domain, and may be used for any purpose whatsover, without permission or notification, but it is offered "as-is" without any warrentee, express or implied. ----------------------------------------------------------------- This generates Wordstar 3 format, what more advanced versions of Wordstar will automatically convert when you edit the file. Andrew D. Todd 1249 Pineview Dr., Apt 4 Morgantown, WV 26505 U46A8@WVNVM.WVNET.EDU */ #include #include #include #include #include int full_tag_spotting_flag, word_wrap_flag; main(int argc, char *argv[]) { int c_i_col, c_o_col, status; int f_in, f_out; char c; if( argc < 3 ) { printf(" \nBoth Source and Destination Files Needed\n"); _exit(3); } f_in = open(argv[1], O_RDONLY|O_BINARY); if(f_in == -1) { printf(" \n%s Does Not Exist\n", argv[1]); _exit(4); } f_out = open(argv[2], O_WRONLY|O_CREAT|O_EXCL|O_BINARY, S_IREAD|S_IWRITE); if( f_out == -1) { printf(" \nDestination File %s Invalid\n", argv[2]); _exit(5); } c_i_col = 0; c_o_col = 0; full_tag_spotting_flag = 1; for(;;) /* Do for the whole file */ { status = read(f_in, &c, 1); if(status != 1) break; /* Check for End of File */ else if( (c == '<') /* LESS THAN */ && ( (full_tag_spotting_flag == 1) || (c_i_col == 0) ) ) c_o_col = digest_tag(f_in, f_out, c_o_col); else if( (c == '&') /* AMPERSAND */ && (full_tag_spotting_flag == 1) ) c_o_col = digest_alias(f_in, f_out, c_o_col); else if( c == '\r') /*CARRIAGE RETURN*/ { c_i_col=0; if(word_wrap_flag == 1) { write(f_out, " ", 1); c_o_col++; } else { write(f_out, " \x8d\n", 3); c_o_col = 0; } } else if( c == '\n') c_i_col=0; /*LINE FEED*/ else if( c == '\a') /*BELL*/ { write(f_out, "[BEL]", 5); c_o_col+=5; } else if( c == '\b') /*BACKSPACE*/ { write(f_out, "[BS]", 4); c_o_col+=4; } else if( c == '\f') /*FORMFEED*/ { write(f_out, "\f", 1); c_i_col = 0; c_o_col = 0; } else if( c == '\t') /*TAB*/ { write(f_out, "\t", 1); c_o_col+=8; } else if( c == '\x1b') /*ESCAPE*/ { write(f_out, "\r\n[ESC]", 7); c_o_col=0; } else if( isprint(c)) /*PRINTABLE*/ { write(f_out, &c, 1); c_i_col++; c_o_col++; } /*always*/ if( (c_o_col > 65) && (c == ' ') && (word_wrap_flag == 1) ) { write(f_out, "\x8d\n", 2); c_o_col = 0; } } close(f_in); close(f_out); _exit(0); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* look_next_char reads the next character then resets the file counter to put it back in the read stream. returns 0 if end of file, 1 if sucessfull */ char look_next_char( int f_in, /* file descriptor for an open System V (low-level) input file */ char *n_char ) { int status; long f_in_position; f_in_position = lseek(f_in, 0L, SEEK_CUR); status = read(f_in, n_char, 1); if(status != 1) return(0); else { lseek(f_in, f_in_position , SEEK_SET); return(1); } } /*--------------------------------------------------------------------------*/ /* ingest_tag ingests everything through the next ending delimiter, and if it does not find a delimiter-marked sequence, outputs the initial delimiter, and resets the file counter to the character following. */ int ingest_tag( int f_in, /* file descriptor for an open System V (low-level) input file */ int f_out, /* ditto for output file */ char startchar, /* starting delimiter */ char endchar, /* ending delimiter */ int perm_len, /* permissible length of tag */ char *build_str /* recieves the tag less delimiters */ ) { int status, not_tag_flag, current_len; char c; long f_in_position; f_in_position = lseek(f_in, 0L, SEEK_CUR); not_tag_flag = 0; build_str[0] = '\0'; for(;;) { current_len = strlen(build_str); status = read(f_in, &c, 1); if(status != 1) { not_tag_flag = 1; break; } else if( !isprint(c)) { not_tag_flag = 1; break; } else if( c == endchar ) { not_tag_flag = 0; break; } else if(current_len > perm_len ) { not_tag_flag = 1; break; } else { build_str[current_len] = c; build_str[current_len+1] = '\0'; } } if(not_tag_flag == 1) { lseek(f_in, f_in_position , SEEK_SET); write(f_out, &startchar, 1); return(0); } else return(1); } /*-------------------------------------------------------------------------*/ int digest_tag( int f_in, /* file descriptor for an open System V (low-level) input file */ int f_out, /* ditto for output file */ int c_o_col /* current output file col */ ) { char build_str[82], send_str[100], c_next; int new_col; char *ptr_last_lf; new_col = 1600; if( ingest_tag(f_in, f_out, '<', '>', 80, build_str) != 1 ) return(c_o_col); /* otherwise, it is a tag */ strupr(build_str); /* ----------- Line Breaks ---------- */ if(strcmp(build_str, "P") == 0) { if( look_next_char(f_in, &c_next) == 1) { if( isprint(c_next) && (c_next != '<') ) strcpy(send_str, "\r\n "); else strcpy(send_str, "\r\n"); } } else if(strcmp(build_str, "/P") == 0) strcpy(send_str, ""); else if(strcmp(build_str, "BR") == 0) strcpy(send_str, "\r\n"); else if(strcmp(build_str, "HR") == 0) { strcpy(send_str, "\r\n--------------------------------------"); strcat(send_str, "---------------------------------------\r\n"); } else if(strcmp(build_str, "LI") == 0) strcpy(send_str, "\r\n\r\n--- "); /* ----------- Character Attributes ---------- */ else if( (strcmp(build_str, "B") == 0) || (strcmp(build_str, "/B") == 0) || (strcmp(build_str, "STRONG") == 0) || (strcmp(build_str, "/STRONG") == 0) ) strcpy(send_str, "\x02"); /* Cntrl - B */ else if( (strcmp(build_str, "I") == 0) || (strcmp(build_str, "/I") == 0) || (strcmp(build_str, "EM") == 0) || (strcmp(build_str, "/EM") == 0) ) strcpy(send_str, "\x19"); /* Cntrl - Y */ else if( (strcmp(build_str, "BLINK") == 0) || (strcmp(build_str, "/BLINK") == 0) || (strcmp(build_str, "U") == 0) || (strcmp(build_str, "/U") == 0) ) strcpy(send_str, "\x13"); /*Render as underline - Cntrl - S */ /* ----------- Text Alignment ---------- */ else if(strcmp(build_str, "CENTER") == 0) strcpy(send_str, "\r\n.ojc\r\n"); else if(strcmp(build_str, "TT") == 0) strcpy(send_str, "\r\n.oj off\r\n"); else if( (strcmp(build_str, "/CENTER") == 0) || (strcmp(build_str, "/TT") == 0) ) strcpy(send_str, "\r\n.oj on\r\n"); else if(strcmp(build_str, "BLOCKQUOTE") == 0) strcpy(send_str, "\r\n.rr1\r\n\r\n"); else if(strcmp(build_str, "/BLOCKQUOTE") == 0) strcpy(send_str, "\r\n\r\n.rr0\r\n"); /* ----------- Character Size ---------- */ else if( (strcmp(build_str, "BIG") == 0) || (strcmp(build_str, "/BIG") == 0) ) strcpy(send_str, ""); else if( (strcmp(build_str, "SMALL") == 0) || (strcmp(build_str, "/SMALL") == 0) ) strcpy(send_str, ""); else if(strcmp(build_str, "/FONT") == 0) strcpy(send_str, ""); else if( (strlen(build_str == 2) ) && ( build_str[0] == "H") && isdigit(build_str[1]) ) { strcpy(send_str, "\r\n[H_"); strcat(send_str, build_str[1]); strcat(send_str, "]\r\n"); } /* ----------- Other Tags ---------- */ else { strcpy(send_str, "\r\n["); strcat(send_str, build_str); strcat(send_str, "]\r\n"); } /* always unless no delimiter pair found, find the last linefeed, and return the current collumn position. */ if( ( ptr_last_lf = strrchr(send_str, '\n') ) != NULL) new_col = send_str + strlen(send_str) - ptr_last_lf - 1; else new_col = c_o_col + strlen(send_str); write( f_out, send_str, strlen(send_str)); return(new_col); } /*--------------------------------------------------------------------------*/ digest_alias( int f_in, /* file descriptor for an open System V (low-level) input file */ int f_out, /* ditto for output file */ c_o_col /* current output file col */ ) { char c, build_str[12], send_str[12]; if( ingest_tag(f_in, f_out, '&', ';', 10, build_str) != 1 ) return(c_o_col); else if(strcmp(build_str, "quot") == 0) strcpy(send_str, "\""); else if(strcmp(build_str, "amp") == 0) strcpy(send_str, "&"); else if(strcmp(build_str, "lt") == 0) strcpy(send_str, "<"); else if(strcmp(build_str, "gt") == 0) strcpy(send_str, ">"); else if(strcmp(build_str, "copy") == 0) strcpy(send_str, "copyright"); else if(strcmp(build_str, "nbsp") == 0) strcpy(send_str, "[nbsp]_"); else if(strcmp(build_str, "#145") == 0) strcpy(send_str, "\'"); else if(strcmp(build_str, "#146") == 0) strcpy(send_str, "\'"); else if(strcmp(build_str, "#147") == 0) strcpy(send_str, "\""); else if(strcmp(build_str, "#148") == 0) strcpy(send_str, "\""); else if(strcmp(build_str, "#149") == 0) strcpy(send_str, "[---]"); /* blank for future expansion else if(strcmp(build_str, "") == 0) strcpy(send_str, ""); */ else if( (strlen(build_str == 4) ) && ( build_str[0] == "#") && isdigit(build_str[1]) && isdigit(build_str[2]) && isdigit(build_str[3]) ) { strcpy(send_str, "[char_"); strcat(send_str, build_str[1]); strcat(send_str, "]"); } else { strcpy(send_str, "["); strcat(send_str, build_str); strcat(send_str, "]"); } /* always unless no delimiter pair found */ write( f_out, send_str, strlen(send_str)); return( c_o_col + strlen(send_str)); }