home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / deskutils / _a_l / boota / C-source / c / ARCtoIBM < prev    next >
Encoding:
Text File  |  1993-04-28  |  19.9 KB  |  496 lines

  1. /* > c.ARCtoIBM - (c) Paul Witheridge - Version 2.02 - 28 Apr 1993   */
  2.  
  3. /*===================================================================*/
  4. /*                                                                   */
  5. /*  ARCtoIBM                                                         */
  6. /*  --------                                                         */
  7. /*                                                                   */
  8. /*  This utility converts ARC ASCII files to IBM ASCII files by:     */
  9. /*                                                                   */
  10. /*    1.  converting LF to CR/LF                                     */
  11. /*    2.  optionally adding a trailing CTRL-Z (0x1A)                 */
  12. /*                                                                   */
  13. /*  It can process multiple files if a wildcarded filename is        */
  14. /*  specified or if a directory name is specified. For a wildcarded  */
  15. /*  filename it processes all files with names that match the        */
  16. /*  pattern. For a directory it processes all files within the       */
  17. /*  directory. Directory names can also be wildcarded.               */
  18. /*                                                                   */
  19. /*-------------------------------------------------------------------*/
  20. /*                                                                   */
  21. /*  COPYRIGHT NOTICE                                                 */
  22. /*                                                                   */
  23. /*  ARCtoIBM is subject to Copyright.                                */
  24. /*                                                                   */
  25. /*  Permission is granted by the author to any recipient of this     */
  26. /*  material to use and make/disseminate copies of the application   */
  27. /*  provided that no charges are made for doing so (other than to    */
  28. /*  cover any cost of media or postage) and that this notice is      */
  29. /*  included with all copies.                                        */
  30. /*                                                                   */
  31. /*===================================================================*/
  32.  
  33. #include "kernel.h"                     /* ARC specifics             */
  34. #include <ctype.h>                      /* Character handling        */
  35. #include <stddef.h>                     /* Standard definitions      */
  36. #include <stdio.h>                      /* Input/output              */
  37. #include <stdlib.h>                     /* General utilities         */
  38. #include <string.h>                     /* String handling           */
  39.  
  40. #include "Beep.h"                       /* Beep header               */
  41. #include "GetDirs.h"                    /* GetDirs header            */
  42. #include "ArgFuncs.h"                   /* Arg functions etc         */
  43. #include "Useful.h"                     /* Useful defns              */
  44.  
  45. /*-------------------------------------------------------------------*/
  46. /*  Global data declarations and definitions.                        */
  47. /*-------------------------------------------------------------------*/
  48.  
  49. #define maxlines 8192                   /* Max no lines to convert   */
  50.  
  51. static unsigned char flags = '\0' ;     /* Processing flags          */
  52.  
  53.   #define flgfnd 0x01                   /* Found file to process     */
  54.   #define flgovr 0x10                   /* Overwrite mode            */
  55.   #define flgrcs 0x20                   /* Recursive mode            */
  56.   #define flgtst 0x40                   /* Test mode                 */
  57.   #define flgx1a 0x80                   /* CTRL-Z required           */
  58.  
  59. static int filecount = 0 ;              /* Count of files processed. */
  60.  
  61. static const char *argptr = NULL ;      /* Pointer to file spec.     */
  62. static const char *pfxptr = "_" ;       /* Pointer to output prefix  */
  63.  
  64. /*-------------------------------------------------------------------*/
  65. /*  Declare functions local in scope to this file.                   */
  66. /*-------------------------------------------------------------------*/
  67.  
  68. static enum boolean cnvtfunc(           /* Convert function          */
  69.   const char *path,
  70.   direntry *ptr) ;
  71.  
  72. /*===================================================================*/
  73. /*                                                                   */
  74. /*  Main program                                                     */
  75. /*  ------------                                                     */
  76. /*                                                                   */
  77. /*  Analyse arguments. Then call the "GetDirentrys" function to      */
  78. /*  read all the file-names in the specified directory (plus any     */
  79. /*  sub directories). Provide "GetDirentrys" with pointer to         */
  80. /*  function to process the specified files.                         */
  81. /*                                                                   */
  82. /*===================================================================*/
  83.  
  84. int main( 
  85.   const int argc,                     /* Number of arguments         */
  86.   const char *argv[])                 /* Array of pointers to args   */
  87. {
  88.   /*-----------------------------------------------------------------*/
  89.   /*  Local definitions.                                             */
  90.   /*-----------------------------------------------------------------*/
  91.  
  92.   int returncode ;
  93.  
  94.   static const char options[] =  /* Used to match option argument    */
  95.   {
  96.     'O',                            /* Overwrite mode                */
  97.     'P',                            /* Output prefix                 */
  98.     'R',                            /* Recursive mode                */
  99.     'T',                            /* Test mode                     */
  100.     'Z',                            /* CTRL-Z required               */
  101.     '0'                             /* Null byte terminator          */
  102.   } ;
  103.  
  104.   static const char **posval[] = /* Ptrs to ptrs to posit'nal values */ 
  105.   {
  106.     &argptr
  107.   } ;
  108.   static const char **(*posargs)[] = &posval ; /* Ptr to array       */
  109.  
  110.   static const unsigned char optflags[] = /* Used to set option flgs */
  111.   {
  112.     flgovr,                         /* Overwrite mode                */
  113.     0,                              /* Output prefix                 */
  114.     flgrcs,                         /* Recursive mode                */
  115.     flgtst,                         /* Test mode                     */
  116.     flgx1a,                         /* CTRL-Z required               */
  117.   } ;
  118.  
  119.   static const char **optval[] = /* Ptrs to ptrs to option values    */
  120.   {
  121.     NULL,                           /* No value allowed              */
  122.     &pfxptr,                        /* Output file prefix            */
  123.     NULL,                           /* No value allowed              */
  124.     NULL,                           /* No value allowed              */
  125.     NULL,                           /* No value allowed              */
  126.   } ;
  127.  
  128.   static const char helpdata[] = /* Help text to be displayed        */
  129.  
  130.     "ARCtoIBM inserts carriage return characters (CR = hex 0D) in "
  131.     "front of all linefeed characters (LF = hex 0A) found in the "
  132.     "file(s) being processed (unless the LF is already preceded by "
  133.     "a CR). It can also optionally add a CTRL-Z (hex 1A) to the end "
  134.     "of each file (unless already present).\x1f"
  135.     "\x1f"
  136.     "WARNING:\x01" "This utility converts files IN-PLACE if the "
  137.     "'-o' option is used to replace the input file with the "
  138.     "converted file.\x1f"
  139.     "\x1f"
  140.     "Syntax:\x01" "*ARCtoIBM  [path.]object  [options]\x1f"
  141.     "\x1f"
  142.     "object:\x01" "Specifies one of the following:\x1f"
  143.     "\x01" "(a) a single non-wildcarded file name\x1f"
  144.     "\x01" "(b) a single non-wildcarded directory name\x1f"
  145.     "\x01" "(c) a wildcarded name.\x1f"
  146.     "\x1f"
  147.     "\x01" "In case (a) the file specified is converted.\x1f"
  148.     "\x1f"
  149.     "\x01" "In case (b) all files in the specified directory are "
  150.     "converted. If the RECURSION option is specified all files "
  151.     "in all subdirectories are also converted\x1f"
  152.     "\x1f"
  153.     "\x01" "In case (c) all matching files are converted. If no "
  154.     "matching files are found, then the first matching directory "
  155.     "name is taken and all files therein are converted. If the "
  156.     "RECURSION option is specified, all matching files are "
  157.     "converted plus all files in all subdirectories.\x1f"
  158.     "\x1f"
  159.     "path:\x01" "Specifies the directory to be searched for the "
  160.     "object file/directory. If omitted the current directory "
  161.     "is searched.\x1f"
  162.     "\x1f"
  163.     "options:\x01" "Specifies processing options which can be one "
  164.     "or more ofthe following:\x1f"
  165.     "\x1f"
  166.     "\x01" "-o\x02" "-\x03" "OVERWRITE mode; output file will have "
  167.     "same name and will replace input file.\x1f"
  168.     "\x1f"
  169.     "\x01" "-p xxx\x02" "-\x03" "specify PREFIX which will be used "
  170.     "to create name for output file if '-o' is not specified; "
  171.     "'xxx' can be one or more characters including directory "
  172.     "specification (e.g. 'converted.'); if not specified a default "
  173.     "of '_' is used.\x1f"
  174.     "\x1f"
  175.     "\x01" "-r\x02" "-\x03" "RECURSION mode which causes all "
  176.     "files in all subdirectories to be converted.\x1f"
  177.     "\x1f"
  178.     "\x01" "-t\x02" "-\x03" "TEST mode; a list of files to be "
  179.     "converted is displayed but no output is actually written; "
  180.     "useful for checking when specifying a directory or "
  181.     "wildcarded filename.\x1f"
  182.     "\x1f"
  183.     "\x01" "-z\x02" "-\x03" "CTRL-Z required; a hex 1A end-of-file "
  184.     "character is added to the end of each file unless already "
  185.     "present.\x1f"
  186.     "\x1f"
  187.     "ARCtoIBM - copyright Paul Witheridge, 1993\x1f"
  188.     "\x1f" ;
  189.  
  190.   static const unsigned char helptabs[] =  /* Help text tab settings */
  191.   {
  192.     1,10,17,19
  193.   } ;
  194.  
  195.   /*-----------------------------------------------------------------*/
  196.   /*  Executable statements                                          */
  197.   /*-----------------------------------------------------------------*/
  198.  
  199.   puts("\nARCtoIBM Version 2.02 - 28 April March 1993\n") ;
  200.  
  201.   /*-----------------------------------------------------------------*/
  202.   /*  Analyse arguments for file-name and option flags.              */
  203.   /*-----------------------------------------------------------------*/
  204.  
  205.   analargs(argc,argv,1,&posargs,options,&flags,optflags,optval) ;
  206.  
  207.   /*-----------------------------------------------------------------*/
  208.   /*  Set paged scrolling mode                                       */
  209.   /*-----------------------------------------------------------------*/
  210.  
  211.   _kernel_oswrch(12) ;
  212.   _kernel_oswrch(14) ;
  213.  
  214.   puts(" PRESS SHIFT KEY TO ALLOW WINDOW TO SCROLL\n") ;
  215.   
  216.   /*-----------------------------------------------------------------*/
  217.   /*  If no file name operand just display help text.                */
  218.   /*-----------------------------------------------------------------*/
  219.  
  220.   if ( argptr == NULL )
  221.   {
  222.     displaytext(helpdata,helptabs) ;
  223.     _kernel_oswrch(15) ;
  224.     return 0 ;
  225.   }
  226.  
  227.   /*-----------------------------------------------------------------*/
  228.   /*  If TEST MODE issue message.                                    */
  229.   /*-----------------------------------------------------------------*/
  230.  
  231.   if ( flags & flgtst )
  232.   {
  233.     puts("TEST MODE (files will be identified, not converted).\n") ;
  234.   }
  235.  
  236.   /*-----------------------------------------------------------------*/
  237.   /*  Invoke GetDirs function to read the directory entry(s) of the  */
  238.   /*  file(s) to be processed. Pass it a pointer of a processing     */
  239.   /*  function to be called.                                         */
  240.   /*-----------------------------------------------------------------*/
  241.  
  242.   returncode = 4 ;
  243.  
  244.   if ( getdirentrys(argptr,
  245.        ( flags & flgrcs ? RECURSE_ALWAYS : RECURSE_ONCE ),cnvtfunc) )
  246.   {
  247.     if ( flags & flgfnd )
  248.     {
  249.       printf("\n%d file(s) ",filecount) ;
  250.       if ( flags & flgtst )
  251.       {
  252.         printf("would be ") ;
  253.       }
  254.       puts("converted.\n") ;
  255.       returncode = 0 ;
  256.     }
  257.     else
  258.     {
  259.       printf("No files found matching '%s'\n",argptr) ;
  260.       beep() ;
  261.     }
  262.   }
  263.   
  264.   /*-----------------------------------------------------------------*/
  265.   /*  Return to caller. All done.                                    */
  266.   /*-----------------------------------------------------------------*/
  267.  
  268.   _kernel_oswrch(15) ;
  269.   return returncode ;
  270. }
  271.  
  272. /*===================================================================*/
  273. /*                                                                   */
  274. /*  cnvtfunc  -  perform actual file conversion                      */
  275. /*  --------                                                         */
  276. /*                                                                   */
  277. /*  This function is called by the "getdirentrys" function for each  */
  278. /*  file-name that it encounters.                                    */
  279. /*                                                                   */
  280. /*===================================================================*/
  281.  
  282. static enum boolean cnvtfunc(
  283.   const char *path,                   /* Pointer to path name.       */
  284.   direntry *ptr)                      /* Pointer to direntry info.   */
  285.  
  286. {
  287.   /*-----------------------------------------------------------------*/
  288.   /*  Local definitions.                                             */
  289.   /*-----------------------------------------------------------------*/
  290.  
  291.   char *infile ;                      /* Ptr to path + leafname      */
  292.   char *oufile ;                      /* Ptr tp path + leafname      */
  293.   int result ;                        /* Result from OS-File SWI     */
  294.   char *workarea ;                    /* Ptr to start of work area   */
  295.   char *workend ;                     /* Ptr to end of work area     */
  296.   char *iptr ;                        /* Working pointer             */
  297.   char *optr ;                        /* Working pointer             */
  298.   register int thischar ;             /* Current character           */
  299.   register int prevchar ;             /* Previous character          */
  300.  
  301.   _kernel_osfile_block osfileblock ;  /* OS_File parameter block     */
  302.  
  303.   /*-----------------------------------------------------------------*/
  304.   /*  Translate table - IBM ASCII to ARC ASCII                       */
  305.   /*-----------------------------------------------------------------*/
  306.   
  307.   static unsigned char trantabl[256] = /* No-op as it  stands        */
  308.   {
  309.       0,   1,   2,   3,   4,   5,   6,   7,
  310.       8,   9,  10,  11,  12,  13,  14,  15,
  311.      16,  17,  18,  19,  20,  21,  22,  23,
  312.      24,  25,  26,  27,  28,  29,  30,  31,
  313.      32,  33,  34,  35,  36,  37,  38,  39,
  314.      40,  41,  42,  43,  44,  45,  46,  47,
  315.      48,  49,  50,  51,  52,  53,  54,  55,
  316.      56,  57,  58,  59,  60,  61,  62,  63,
  317.      64,  65,  66,  67,  68,  69,  70,  71,
  318.      72,  73,  74,  75,  76,  77,  78,  79,
  319.      80,  81,  82,  83,  84,  85,  86,  87,
  320.      88,  89,  90,  91,  92,  93,  94,  95,
  321.      96,  97,  98,  99, 100, 101, 102, 103,
  322.     104, 105, 106, 107, 108, 109, 110, 111,
  323.     112, 113, 114, 115, 116, 117, 118, 119,
  324.     120, 121, 122, 123, 124, 125, 126, 127,
  325.     128, 129, 130, 131, 132, 133, 134, 135,
  326.     136, 137, 138, 139, 140, 141, 142, 143,
  327.     144, 145, 146, 147, 148, 149, 150, 151,
  328.     152, 153, 154, 155, 156, 157, 158, 159,
  329.     160, 161, 162, 163, 164, 165, 166, 167,
  330.     168, 169, 170, 171, 172, 173, 174, 175,
  331.     176, 177, 178, 179, 180, 181, 182, 183,
  332.     184, 185, 186, 187, 188, 189, 190, 191,
  333.     192, 193, 194, 195, 196, 197, 198, 199,
  334.     200, 201, 202, 203, 204, 205, 206, 207,
  335.     208, 209, 210, 211, 212, 213, 214, 215,
  336.     216, 217, 218, 219, 220, 221, 222, 223,
  337.     224, 225, 226, 227, 228, 229, 230, 231,
  338.     232, 233, 234, 235, 236, 237, 238, 239,
  339.     240, 241, 242, 243, 244, 245, 246, 247,
  340.     248, 249, 250, 251, 252, 253, 254, 255 
  341.   } ;
  342.  
  343.   /*-----------------------------------------------------------------*/
  344.   /*  If test mode display heading on first time through             */
  345.   /*-----------------------------------------------------------------*/
  346.  
  347.   if ( (flags & flgtst) && !(flags & flgfnd) )
  348.   {
  349.     puts("The following files would be converted:\n") ;
  350.   }
  351.  
  352.   flags |= flgfnd ;
  353.  
  354.   /*-----------------------------------------------------------------*/
  355.   /*  Create input and output filespecs                              */
  356.   /*-----------------------------------------------------------------*/
  357.   
  358.   if ( ( infile = malloc(2 * (strlen(path) + strlen(ptr->name) + 1)
  359.                          + strlen(pfxptr) ) ) == NULL )
  360.   {
  361.     puts("Out of memory") ;
  362.     return FALSE ;
  363.   } 
  364.   
  365.   oufile = infile + sprintf(infile,"%s%s",path,ptr->name) + 1 ;
  366.   sprintf(oufile,"%s%s%s",path,flags & flgovr ? "" : pfxptr,ptr->name) ;
  367.   
  368.   /*-----------------------------------------------------------------*/
  369.   /*  Check input file has non-zero length                           */
  370.   /*-----------------------------------------------------------------*/
  371.  
  372.   if ( ptr->length == 0 )
  373.   {
  374.     printf("'%s' is empty\n",infile) ;
  375.   }
  376.  
  377.   /*-----------------------------------------------------------------*/
  378.   /*  Allocate memory for file and load file                         */
  379.   /*-----------------------------------------------------------------*/
  380.  
  381.   if ( (workarea = malloc((ptr->length) + maxlines + 1) ) == NULL )
  382.   {
  383.     printf("'%s' too large to load",infile) ;
  384.     goto error4 ;
  385.   }
  386.  
  387.   optr = workarea ;
  388.   iptr = workarea + maxlines ;
  389.   workend = iptr + ptr->length ;
  390.  
  391.   osfileblock.load  = (int)iptr ;
  392.   osfileblock.exec  = 0 ;
  393.   result = _kernel_osfile(16,infile,&osfileblock) ;
  394.  
  395.   if (result == _kernel_ERROR)
  396.   {
  397.     printf("'%s' load failed - %s\n",infile,
  398.           _kernel_last_oserror()->errmess) ;
  399.     goto error3 ;
  400.   }
  401.  
  402.   /*-----------------------------------------------------------------*/
  403.   /*  Convert all LF to CR/LF and add trailing CTRL-Z if required.   */
  404.   /*-----------------------------------------------------------------*/
  405.  
  406.   prevchar = 0 ;
  407.   
  408.   while ( iptr < workend )
  409.   {
  410.     *optr = thischar = trantabl[*iptr] ;
  411.     iptr++ ;
  412.     if ( thischar == 0x0A && prevchar != 0x0D )
  413.     {
  414.       *optr = 0x0D ;
  415.       optr++ ;
  416.       if ( optr >= iptr )
  417.       {
  418.         printf("'%s' conversion failed - too many lines\n",infile) ;
  419.         goto error3 ;
  420.       }
  421.       *optr = 0x0A ;
  422.     }
  423.     optr++ ;
  424.     prevchar = thischar ;
  425.   }
  426.   
  427.   if ( flags & flgx1a && prevchar != 0x1A )
  428.   {
  429.     *optr = 0x1A ;
  430.     optr++ ;
  431.   }  
  432.   
  433.   /*-----------------------------------------------------------------*/
  434.   /*  If not test mode, save modified file                           */
  435.   /*-----------------------------------------------------------------*/
  436.  
  437.   if ( !(flags & flgtst) )
  438.   {
  439.     osfileblock.load = ptr->load ;
  440.     osfileblock.exec   = ptr->exec ;
  441.     osfileblock.start  = (int)workarea ;
  442.     osfileblock.end    = (int)optr ;
  443.  
  444.     result = _kernel_osfile(0,oufile,&osfileblock) ;
  445.  
  446.     if (result == _kernel_ERROR)
  447.     {
  448.       printf("'%s' save failed - %s\n",oufile,
  449.              _kernel_last_oserror()->errmess);
  450.       goto error3 ;
  451.     }
  452.   }
  453.  
  454.   /*-----------------------------------------------------------------*/
  455.   /*  Valid conversion - bump file count                             */
  456.   /*-----------------------------------------------------------------*/
  457.   
  458.   filecount++ ;
  459.  
  460.   /*-----------------------------------------------------------------*/
  461.   /*  Display file name (plus "converted" if not test mode)          */
  462.   /*-----------------------------------------------------------------*/
  463.  
  464.   printf("'%s'",infile) ;
  465.   if ( !(flags & flgtst) )
  466.   {
  467.     printf(" converted") ;
  468.     if ( !(flags & flgovr) )
  469.     {
  470.       printf(" and saved as '%s'",oufile) ;
  471.     }
  472.   }
  473.   putchar('\n') ;
  474.  
  475.   /*-----------------------------------------------------------------*/
  476.   /*  Free work area and return with good completion code            */
  477.   /*-----------------------------------------------------------------*/
  478.  
  479.   free(workarea) ;
  480.   free(infile) ;
  481.  
  482.   return TRUE ;
  483.  
  484.   /*-----------------------------------------------------------------*/
  485.   /*  Error exits                                                    */
  486.   /*-----------------------------------------------------------------*/
  487.  
  488.   error3: free(workarea) ;
  489.   error4: free(infile) ;
  490.           beep() ;
  491.  
  492.   return FALSE ;
  493. }
  494.  
  495. /*=====================================================================*/
  496.