home *** CD-ROM | disk | FTP | other *** search
- /* > c.ARCtoIBM - (c) Paul Witheridge - Version 2.02 - 28 Apr 1993 */
-
- /*===================================================================*/
- /* */
- /* ARCtoIBM */
- /* -------- */
- /* */
- /* This utility converts ARC ASCII files to IBM ASCII files by: */
- /* */
- /* 1. converting LF to CR/LF */
- /* 2. optionally adding a trailing CTRL-Z (0x1A) */
- /* */
- /* It can process multiple files if a wildcarded filename is */
- /* specified or if a directory name is specified. For a wildcarded */
- /* filename it processes all files with names that match the */
- /* pattern. For a directory it processes all files within the */
- /* directory. Directory names can also be wildcarded. */
- /* */
- /*-------------------------------------------------------------------*/
- /* */
- /* COPYRIGHT NOTICE */
- /* */
- /* ARCtoIBM is subject to Copyright. */
- /* */
- /* Permission is granted by the author to any recipient of this */
- /* material to use and make/disseminate copies of the application */
- /* provided that no charges are made for doing so (other than to */
- /* cover any cost of media or postage) and that this notice is */
- /* included with all copies. */
- /* */
- /*===================================================================*/
-
- #include "kernel.h" /* ARC specifics */
- #include <ctype.h> /* Character handling */
- #include <stddef.h> /* Standard definitions */
- #include <stdio.h> /* Input/output */
- #include <stdlib.h> /* General utilities */
- #include <string.h> /* String handling */
-
- #include "Beep.h" /* Beep header */
- #include "GetDirs.h" /* GetDirs header */
- #include "ArgFuncs.h" /* Arg functions etc */
- #include "Useful.h" /* Useful defns */
-
- /*-------------------------------------------------------------------*/
- /* Global data declarations and definitions. */
- /*-------------------------------------------------------------------*/
-
- #define maxlines 8192 /* Max no lines to convert */
-
- static unsigned char flags = '\0' ; /* Processing flags */
-
- #define flgfnd 0x01 /* Found file to process */
- #define flgovr 0x10 /* Overwrite mode */
- #define flgrcs 0x20 /* Recursive mode */
- #define flgtst 0x40 /* Test mode */
- #define flgx1a 0x80 /* CTRL-Z required */
-
- static int filecount = 0 ; /* Count of files processed. */
-
- static const char *argptr = NULL ; /* Pointer to file spec. */
- static const char *pfxptr = "_" ; /* Pointer to output prefix */
-
- /*-------------------------------------------------------------------*/
- /* Declare functions local in scope to this file. */
- /*-------------------------------------------------------------------*/
-
- static enum boolean cnvtfunc( /* Convert function */
- const char *path,
- direntry *ptr) ;
-
- /*===================================================================*/
- /* */
- /* Main program */
- /* ------------ */
- /* */
- /* Analyse arguments. Then call the "GetDirentrys" function to */
- /* read all the file-names in the specified directory (plus any */
- /* sub directories). Provide "GetDirentrys" with pointer to */
- /* function to process the specified files. */
- /* */
- /*===================================================================*/
-
- int main(
- const int argc, /* Number of arguments */
- const char *argv[]) /* Array of pointers to args */
- {
- /*-----------------------------------------------------------------*/
- /* Local definitions. */
- /*-----------------------------------------------------------------*/
-
- int returncode ;
-
- static const char options[] = /* Used to match option argument */
- {
- 'O', /* Overwrite mode */
- 'P', /* Output prefix */
- 'R', /* Recursive mode */
- 'T', /* Test mode */
- 'Z', /* CTRL-Z required */
- '0' /* Null byte terminator */
- } ;
-
- static const char **posval[] = /* Ptrs to ptrs to posit'nal values */
- {
- &argptr
- } ;
- static const char **(*posargs)[] = &posval ; /* Ptr to array */
-
- static const unsigned char optflags[] = /* Used to set option flgs */
- {
- flgovr, /* Overwrite mode */
- 0, /* Output prefix */
- flgrcs, /* Recursive mode */
- flgtst, /* Test mode */
- flgx1a, /* CTRL-Z required */
- } ;
-
- static const char **optval[] = /* Ptrs to ptrs to option values */
- {
- NULL, /* No value allowed */
- &pfxptr, /* Output file prefix */
- NULL, /* No value allowed */
- NULL, /* No value allowed */
- NULL, /* No value allowed */
- } ;
-
- static const char helpdata[] = /* Help text to be displayed */
-
- "ARCtoIBM inserts carriage return characters (CR = hex 0D) in "
- "front of all linefeed characters (LF = hex 0A) found in the "
- "file(s) being processed (unless the LF is already preceded by "
- "a CR). It can also optionally add a CTRL-Z (hex 1A) to the end "
- "of each file (unless already present).\x1f"
- "\x1f"
- "WARNING:\x01" "This utility converts files IN-PLACE if the "
- "'-o' option is used to replace the input file with the "
- "converted file.\x1f"
- "\x1f"
- "Syntax:\x01" "*ARCtoIBM [path.]object [options]\x1f"
- "\x1f"
- "object:\x01" "Specifies one of the following:\x1f"
- "\x01" "(a) a single non-wildcarded file name\x1f"
- "\x01" "(b) a single non-wildcarded directory name\x1f"
- "\x01" "(c) a wildcarded name.\x1f"
- "\x1f"
- "\x01" "In case (a) the file specified is converted.\x1f"
- "\x1f"
- "\x01" "In case (b) all files in the specified directory are "
- "converted. If the RECURSION option is specified all files "
- "in all subdirectories are also converted\x1f"
- "\x1f"
- "\x01" "In case (c) all matching files are converted. If no "
- "matching files are found, then the first matching directory "
- "name is taken and all files therein are converted. If the "
- "RECURSION option is specified, all matching files are "
- "converted plus all files in all subdirectories.\x1f"
- "\x1f"
- "path:\x01" "Specifies the directory to be searched for the "
- "object file/directory. If omitted the current directory "
- "is searched.\x1f"
- "\x1f"
- "options:\x01" "Specifies processing options which can be one "
- "or more ofthe following:\x1f"
- "\x1f"
- "\x01" "-o\x02" "-\x03" "OVERWRITE mode; output file will have "
- "same name and will replace input file.\x1f"
- "\x1f"
- "\x01" "-p xxx\x02" "-\x03" "specify PREFIX which will be used "
- "to create name for output file if '-o' is not specified; "
- "'xxx' can be one or more characters including directory "
- "specification (e.g. 'converted.'); if not specified a default "
- "of '_' is used.\x1f"
- "\x1f"
- "\x01" "-r\x02" "-\x03" "RECURSION mode which causes all "
- "files in all subdirectories to be converted.\x1f"
- "\x1f"
- "\x01" "-t\x02" "-\x03" "TEST mode; a list of files to be "
- "converted is displayed but no output is actually written; "
- "useful for checking when specifying a directory or "
- "wildcarded filename.\x1f"
- "\x1f"
- "\x01" "-z\x02" "-\x03" "CTRL-Z required; a hex 1A end-of-file "
- "character is added to the end of each file unless already "
- "present.\x1f"
- "\x1f"
- "ARCtoIBM - copyright Paul Witheridge, 1993\x1f"
- "\x1f" ;
-
- static const unsigned char helptabs[] = /* Help text tab settings */
- {
- 1,10,17,19
- } ;
-
- /*-----------------------------------------------------------------*/
- /* Executable statements */
- /*-----------------------------------------------------------------*/
-
- puts("\nARCtoIBM Version 2.02 - 28 April March 1993\n") ;
-
- /*-----------------------------------------------------------------*/
- /* Analyse arguments for file-name and option flags. */
- /*-----------------------------------------------------------------*/
-
- analargs(argc,argv,1,&posargs,options,&flags,optflags,optval) ;
-
- /*-----------------------------------------------------------------*/
- /* Set paged scrolling mode */
- /*-----------------------------------------------------------------*/
-
- _kernel_oswrch(12) ;
- _kernel_oswrch(14) ;
-
- puts(" PRESS SHIFT KEY TO ALLOW WINDOW TO SCROLL\n") ;
-
- /*-----------------------------------------------------------------*/
- /* If no file name operand just display help text. */
- /*-----------------------------------------------------------------*/
-
- if ( argptr == NULL )
- {
- displaytext(helpdata,helptabs) ;
- _kernel_oswrch(15) ;
- return 0 ;
- }
-
- /*-----------------------------------------------------------------*/
- /* If TEST MODE issue message. */
- /*-----------------------------------------------------------------*/
-
- if ( flags & flgtst )
- {
- puts("TEST MODE (files will be identified, not converted).\n") ;
- }
-
- /*-----------------------------------------------------------------*/
- /* Invoke GetDirs function to read the directory entry(s) of the */
- /* file(s) to be processed. Pass it a pointer of a processing */
- /* function to be called. */
- /*-----------------------------------------------------------------*/
-
- returncode = 4 ;
-
- if ( getdirentrys(argptr,
- ( flags & flgrcs ? RECURSE_ALWAYS : RECURSE_ONCE ),cnvtfunc) )
- {
- if ( flags & flgfnd )
- {
- printf("\n%d file(s) ",filecount) ;
- if ( flags & flgtst )
- {
- printf("would be ") ;
- }
- puts("converted.\n") ;
- returncode = 0 ;
- }
- else
- {
- printf("No files found matching '%s'\n",argptr) ;
- beep() ;
- }
- }
-
- /*-----------------------------------------------------------------*/
- /* Return to caller. All done. */
- /*-----------------------------------------------------------------*/
-
- _kernel_oswrch(15) ;
- return returncode ;
- }
-
- /*===================================================================*/
- /* */
- /* cnvtfunc - perform actual file conversion */
- /* -------- */
- /* */
- /* This function is called by the "getdirentrys" function for each */
- /* file-name that it encounters. */
- /* */
- /*===================================================================*/
-
- static enum boolean cnvtfunc(
- const char *path, /* Pointer to path name. */
- direntry *ptr) /* Pointer to direntry info. */
-
- {
- /*-----------------------------------------------------------------*/
- /* Local definitions. */
- /*-----------------------------------------------------------------*/
-
- char *infile ; /* Ptr to path + leafname */
- char *oufile ; /* Ptr tp path + leafname */
- int result ; /* Result from OS-File SWI */
- char *workarea ; /* Ptr to start of work area */
- char *workend ; /* Ptr to end of work area */
- char *iptr ; /* Working pointer */
- char *optr ; /* Working pointer */
- register int thischar ; /* Current character */
- register int prevchar ; /* Previous character */
-
- _kernel_osfile_block osfileblock ; /* OS_File parameter block */
-
- /*-----------------------------------------------------------------*/
- /* Translate table - IBM ASCII to ARC ASCII */
- /*-----------------------------------------------------------------*/
-
- static unsigned char trantabl[256] = /* No-op as it stands */
- {
- 0, 1, 2, 3, 4, 5, 6, 7,
- 8, 9, 10, 11, 12, 13, 14, 15,
- 16, 17, 18, 19, 20, 21, 22, 23,
- 24, 25, 26, 27, 28, 29, 30, 31,
- 32, 33, 34, 35, 36, 37, 38, 39,
- 40, 41, 42, 43, 44, 45, 46, 47,
- 48, 49, 50, 51, 52, 53, 54, 55,
- 56, 57, 58, 59, 60, 61, 62, 63,
- 64, 65, 66, 67, 68, 69, 70, 71,
- 72, 73, 74, 75, 76, 77, 78, 79,
- 80, 81, 82, 83, 84, 85, 86, 87,
- 88, 89, 90, 91, 92, 93, 94, 95,
- 96, 97, 98, 99, 100, 101, 102, 103,
- 104, 105, 106, 107, 108, 109, 110, 111,
- 112, 113, 114, 115, 116, 117, 118, 119,
- 120, 121, 122, 123, 124, 125, 126, 127,
- 128, 129, 130, 131, 132, 133, 134, 135,
- 136, 137, 138, 139, 140, 141, 142, 143,
- 144, 145, 146, 147, 148, 149, 150, 151,
- 152, 153, 154, 155, 156, 157, 158, 159,
- 160, 161, 162, 163, 164, 165, 166, 167,
- 168, 169, 170, 171, 172, 173, 174, 175,
- 176, 177, 178, 179, 180, 181, 182, 183,
- 184, 185, 186, 187, 188, 189, 190, 191,
- 192, 193, 194, 195, 196, 197, 198, 199,
- 200, 201, 202, 203, 204, 205, 206, 207,
- 208, 209, 210, 211, 212, 213, 214, 215,
- 216, 217, 218, 219, 220, 221, 222, 223,
- 224, 225, 226, 227, 228, 229, 230, 231,
- 232, 233, 234, 235, 236, 237, 238, 239,
- 240, 241, 242, 243, 244, 245, 246, 247,
- 248, 249, 250, 251, 252, 253, 254, 255
- } ;
-
- /*-----------------------------------------------------------------*/
- /* If test mode display heading on first time through */
- /*-----------------------------------------------------------------*/
-
- if ( (flags & flgtst) && !(flags & flgfnd) )
- {
- puts("The following files would be converted:\n") ;
- }
-
- flags |= flgfnd ;
-
- /*-----------------------------------------------------------------*/
- /* Create input and output filespecs */
- /*-----------------------------------------------------------------*/
-
- if ( ( infile = malloc(2 * (strlen(path) + strlen(ptr->name) + 1)
- + strlen(pfxptr) ) ) == NULL )
- {
- puts("Out of memory") ;
- return FALSE ;
- }
-
- oufile = infile + sprintf(infile,"%s%s",path,ptr->name) + 1 ;
- sprintf(oufile,"%s%s%s",path,flags & flgovr ? "" : pfxptr,ptr->name) ;
-
- /*-----------------------------------------------------------------*/
- /* Check input file has non-zero length */
- /*-----------------------------------------------------------------*/
-
- if ( ptr->length == 0 )
- {
- printf("'%s' is empty\n",infile) ;
- }
-
- /*-----------------------------------------------------------------*/
- /* Allocate memory for file and load file */
- /*-----------------------------------------------------------------*/
-
- if ( (workarea = malloc((ptr->length) + maxlines + 1) ) == NULL )
- {
- printf("'%s' too large to load",infile) ;
- goto error4 ;
- }
-
- optr = workarea ;
- iptr = workarea + maxlines ;
- workend = iptr + ptr->length ;
-
- osfileblock.load = (int)iptr ;
- osfileblock.exec = 0 ;
- result = _kernel_osfile(16,infile,&osfileblock) ;
-
- if (result == _kernel_ERROR)
- {
- printf("'%s' load failed - %s\n",infile,
- _kernel_last_oserror()->errmess) ;
- goto error3 ;
- }
-
- /*-----------------------------------------------------------------*/
- /* Convert all LF to CR/LF and add trailing CTRL-Z if required. */
- /*-----------------------------------------------------------------*/
-
- prevchar = 0 ;
-
- while ( iptr < workend )
- {
- *optr = thischar = trantabl[*iptr] ;
- iptr++ ;
- if ( thischar == 0x0A && prevchar != 0x0D )
- {
- *optr = 0x0D ;
- optr++ ;
- if ( optr >= iptr )
- {
- printf("'%s' conversion failed - too many lines\n",infile) ;
- goto error3 ;
- }
- *optr = 0x0A ;
- }
- optr++ ;
- prevchar = thischar ;
- }
-
- if ( flags & flgx1a && prevchar != 0x1A )
- {
- *optr = 0x1A ;
- optr++ ;
- }
-
- /*-----------------------------------------------------------------*/
- /* If not test mode, save modified file */
- /*-----------------------------------------------------------------*/
-
- if ( !(flags & flgtst) )
- {
- osfileblock.load = ptr->load ;
- osfileblock.exec = ptr->exec ;
- osfileblock.start = (int)workarea ;
- osfileblock.end = (int)optr ;
-
- result = _kernel_osfile(0,oufile,&osfileblock) ;
-
- if (result == _kernel_ERROR)
- {
- printf("'%s' save failed - %s\n",oufile,
- _kernel_last_oserror()->errmess);
- goto error3 ;
- }
- }
-
- /*-----------------------------------------------------------------*/
- /* Valid conversion - bump file count */
- /*-----------------------------------------------------------------*/
-
- filecount++ ;
-
- /*-----------------------------------------------------------------*/
- /* Display file name (plus "converted" if not test mode) */
- /*-----------------------------------------------------------------*/
-
- printf("'%s'",infile) ;
- if ( !(flags & flgtst) )
- {
- printf(" converted") ;
- if ( !(flags & flgovr) )
- {
- printf(" and saved as '%s'",oufile) ;
- }
- }
- putchar('\n') ;
-
- /*-----------------------------------------------------------------*/
- /* Free work area and return with good completion code */
- /*-----------------------------------------------------------------*/
-
- free(workarea) ;
- free(infile) ;
-
- return TRUE ;
-
- /*-----------------------------------------------------------------*/
- /* Error exits */
- /*-----------------------------------------------------------------*/
-
- error3: free(workarea) ;
- error4: free(infile) ;
- beep() ;
-
- return FALSE ;
- }
-
- /*=====================================================================*/
-