home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / filedocs / simcvt2.c < prev    next >
C/C++ Source or Header  |  1994-03-04  |  7KB  |  195 lines

  1. /*
  2. I have uploaded a new version of simcvt.c for <msdos.filedocs>.
  3. It makes the file grep.sim (also written by me) unnecessary.
  4. It uses regex to reduce the display only to files of interest.
  5. It can be made to automagically call your pager, and can do
  6. case-insensitive searches.  It allows the input filename to be
  7. specified on the command line, and can use a default.
  8.  
  9. My changes are public domain.  -David-
  10.  
  11. # david@wubios.wustl.edu             ^     Mr. David J. Camp
  12. # david%wubios@wugate.wustl.edu    < * >   +1 314 382 0584
  13. # ...!uunet!wugate!wubios!david      v     "God loves material things."
  14. # abs (investment#1 - investment#2) << abs (anyinvestment - anydebt)
  15. */
  16.  
  17. /*****************************************************************************
  18.  
  19. Written by reynolds@sun.com                                     01/21/90
  20. Minor corrections to instructions and portability
  21.         davidsen@crdos1.crd.ge.com            02/23/90
  22.  
  23. This SIMCVT.C filter should convert Simtel-20's "SIMIBM.IDX" file into a
  24. readable "SIMIBM.LST" that is compatible with the other convert programs,
  25. except for the run-date at the top of the output file.
  26.  
  27. This program, written in "C" should compile on both 4.3BSD Unix machines,
  28. as well as IBM/pc compatible machines.  It works on both VAXen, and Suns.
  29.  
  30. To Compile on Unix, type "cc -o simcvt SIMCVT.C" creating simcvt.
  31. To Compile on IBM/pcs, see your C manual that came with the compiler.
  32.  
  33. To run, type "simcvt < simibm.idx > simibm.lst
  34.  
  35. ******************************************************************************/
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. char * re_comp ();
  41. int re_exec ();
  42.  
  43. void
  44. main (argc, argv)
  45. int argc;
  46. char * argv [];
  47.  
  48. {
  49. char  fs[10],dir[60],name[15],descr[60]; /* input variables */
  50. char  inputline[257];                    /* for initial read */
  51. char  lowerline[257];                    /* inputline converted to lowercase */
  52. int   rev,bits;                          /* input variables */
  53. long  length,date;                       /* input variables */
  54. char  lfs[10],ldir[60];                  /* stores last filesystem/directory */
  55. char  type;                              /* output variable for 'A' or 'B' */
  56. char  c;                                 /* picks off EOF,",linefeed */
  57. FILE * output_file;                      /* output file handle */
  58. char * pager_name;                       /* name of pager */
  59. char * error_message;                    /* re_comp return value */
  60. char * pattern;                          /* pattern to match */
  61. int   arg_flag;                          /* command line flag */
  62. extern char *optarg;                     /* optional argument string */
  63. extern int optind;                       /* arg number being scanned */
  64. int    insensitive;                      /* do search case-insensitively */
  65. char * ptr;                              /* temporary ptr for scanning */
  66. char * input_filename;                   /* name of input file */
  67. FILE * input_file;                       /* handle for input file */
  68.  
  69. input_filename = NULL;
  70. insensitive = 0;
  71. output_file = stdout;
  72. pager_name = NULL;
  73. while ((arg_flag = getopt (argc, argv, "Ff:ip")) != -1)
  74.     {
  75.     switch (arg_flag) 
  76.         {
  77.     case 'F':
  78.         input_filename = getenv ("IDX");
  79.         break;
  80.     case 'f':
  81.         input_filename = optarg;
  82.         break;
  83.     case 'i':
  84.         insensitive = 1;
  85.         break;
  86.     case 'p':
  87.         pager_name = getenv ("PAGER");
  88.         if (pager_name == NULL)
  89.             pager_name = "more";    
  90.         break;
  91.         }
  92.     }
  93. if (optind < argc)
  94.     {
  95.     pattern = argv [optind];    
  96.     optind ++;
  97.     }
  98. else
  99.     pattern = ".*";
  100. if (input_filename)
  101.     {
  102.     input_file = fopen (input_filename, "r");
  103.     if (input_file == NULL)
  104.         {
  105.         fprintf (stderr, "Cannot open file: %s\n", input_filename);
  106.         exit (3);
  107.         }
  108.     }
  109. else if (isatty (fileno (stdin)))
  110.     {
  111.     fprintf (stderr, 
  112. "Usage: simcvt [-f filename] [-Fip] [regex-pattern] [ < /path/simibm.idx]\n");
  113.     fprintf (stderr,
  114. "The simibm.idx file is taken from the standard input, \n");
  115.     fprintf (stderr, 
  116. "or from the file specified by the -f option.\n");
  117.     fprintf (stderr, 
  118. "The -F option selects the input file identied by the IDX variable.\n");
  119.     fprintf (stderr, 
  120. "The -i option makes pattern matching case-insensitive.\n");
  121.     fprintf (stderr,
  122. "The -p option will gate the printout to your pager.\n");
  123.     fprintf (stderr,
  124. "For example: simcvt -Fip \"pattern\"\n");
  125.     pclose (output_file);
  126.     exit (2);
  127.     }
  128. else
  129.     input_file = stdin;
  130. if (insensitive)
  131.     {
  132.     for (ptr = pattern; *ptr; ptr++)
  133.         {
  134.         if (isalpha (*ptr))
  135.             *ptr = tolower (*ptr);
  136.         }
  137.     }
  138. error_message = re_comp (pattern);
  139. if (error_message != NULL)
  140.     fprintf (stderr, "Regular Expression Error: %s\n", error_message);
  141. if (pager_name != NULL)
  142.     {
  143.     output_file = popen (pager_name, "w");
  144.     if (output_file == NULL)
  145.         output_file = stdout;
  146.     }
  147. fprintf (output_file, "SimTel MS-DOS Files Listing\n\n");
  148. fprintf (output_file, "NOTE: Type B is Binary; Type A is ASCII\n");
  149. inputline[256] = 0;
  150. while (fgets (inputline, 256, input_file) != NULL)
  151.     {
  152.     strcpy (lowerline, inputline);
  153.     if (insensitive)
  154.         {
  155.         for (ptr = lowerline; *ptr; ptr++)
  156.         {
  157.             if (isalpha (*ptr))
  158.                 *ptr = tolower (*ptr);
  159.         }
  160.         }
  161.     if (re_exec (lowerline))
  162.         {
  163.         sscanf(inputline, 
  164.                 "\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",%d,%ld,%d,%ld,\"%[^\"]\"",
  165.                 fs, dir, name, &rev, &length, &bits, &date, descr);
  166.         type = 'B';                           /* Binary 8-bit */
  167.         if (bits == 7) type = 'A';            /* ASCII  7-bit */
  168.         if (strcmp(ldir,dir) || strcmp(lfs,fs)) 
  169.             {  /* New Directory */
  170.             fprintf (output_file, "\nDirectory %s%s\n",fs,dir);
  171.             fprintf (output_file, 
  172.                     " Filename   Type Length   Date    Description\n");
  173.             fprintf (output_file, 
  174.                     "==============================================\n");
  175.             strcpy(ldir, dir);        /* Remember last directory with ldir  */
  176.             strcpy(lfs,fs);           /* Remember last file system with lfs */
  177.             }                         /* End of the New Directory routine   */
  178.         fprintf (output_file, 
  179.                 "%-12.12s  %c %7ld  %6ld  %s\n",name,type,length,date,descr);
  180.         }
  181.     }
  182. if (output_file != stdout)
  183.     pclose (output_file);
  184. } /* end of main() program by Ray */
  185.  
  186. /*****************************************************************************
  187.  
  188.    This filter takes data in the following format:
  189. "PD1:","<MSDOS.ADA>","ADA-LRM2.ARC",1,320086,8,890411,"The Ada Language Reference Manual reader (2/4)"
  190.  
  191.    And converts it to the following format:
  192. ADA-LRM1.ARC  B  231947  890411  The Ada Language Reference Manual reader (1/4)
  193.  
  194. *****************************************************************************/
  195.