home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / deskutils / _a_l / boota / C-source / c / IBMtoARC < prev    next >
Encoding:
Text File  |  1993-03-03  |  18.9 KB  |  471 lines

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