home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DOLIST.CMD < prev    next >
OS/2 REXX Batch file  |  1995-05-19  |  3KB  |  107 lines

  1. EXTPROC CEnvi2
  2. //*************************************************************
  3. //*** DoList.cmd - perform some action on every element     ***
  4. //*** ver.2        in a list.  The list is taken from       ***
  5. //***              standard input or if a file is specified ***
  6. //***              then from that file.                     ***
  7. //*************************************************************
  8.  
  9. #include <OptParms.lib>
  10.  
  11. #define INSERT_SEQUENCE  "$$$"
  12.  
  13. main(argc,argv)
  14. {
  15.    Skip = OptionalParameter(argc,argv,"SKIP",Temp) ? atoi(Temp) : 0 ;
  16.    Truncate = OptionalParameter(argc,argv,"TRUNC",Temp) ? atoi(Temp) : 0 ;
  17.    if ( argc < 2  ||  3 < argc
  18.      || OptionalParameter(argc,argv,"?")
  19.      || OptionalParameter(argc,argv,"help")
  20.      || OptionalParameter(argc,argv,"h") ) {
  21.       Instructions();
  22.       exit(EXIT_FAILURE);
  23.    }
  24.  
  25.    Action = argv[1];
  26.  
  27.    if ( argc == 2 ) {
  28.       // take input from stdin
  29.       fp = stdin;
  30.    } else {
  31.       fp = fopen(argv[2],"rt");
  32.       if ( !fp ) {
  33.          printf("\aUnable to open \"%s\" for reading.\n",argv[2]);
  34.          exit(EXIT_FAILURE);
  35.       }
  36.    }
  37.  
  38.    ActOnEachLine(Action,fp,Skip,Truncate);
  39.  
  40.    if ( argc != 2 )
  41.       fclose(fp);
  42. }
  43.  
  44. ActOnEachLine(Action,fp,Skip,Truncate)
  45. {
  46.    while( (line = fgets(fp)) ) {
  47.  
  48.       // get rid of neline at end
  49.       LineLen = strlen(line);
  50.       if ( line[LineLen-1] == '\n' )
  51.          line[--LineLen] == '\0';
  52.  
  53.       // remove Skip and Truncate text if applicable
  54.       if ( Skip <= LineLen ) {
  55.          line += Skip;
  56.          LineLen -= Skip;
  57.          if ( Truncate <= LineLen ) {
  58.             line[LineLen -= Truncate] = '\0';
  59.             if ( LineLen ) {
  60.                ActOnLine(Action,line);
  61.             }
  62.          }
  63.       }
  64.    }
  65. }
  66.  
  67. ActOnLine(Action,Line)  // act on line, including INSERT_SEQUENCE
  68. {
  69.    // create statement replacing INSERT_SEQUENCE with line, or putting
  70.    // line at end if no INSERT_SEQUENCE
  71.    strcpy(lCmd,Action);
  72.  
  73.    // If insert sequence then replace with Line, else concatenate line
  74.    if ( insert = strstr(lCmd,INSERT_SEQUENCE) ) {
  75.       do {
  76.          strcpy(insert + strlen(Line),insert+strlen(INSERT_SEQUENCE));
  77.          memcpy(insert,Line,strlen(Line));
  78.       } while ( insert = strstr(insert + strlen(Line),INSERT_SEQUENCE) );
  79.    } else {
  80.       strcat(lCmd," ");
  81.       strcat(lCmd,Line);
  82.    }
  83.  
  84.    // show and execute the command
  85.    printf("%s\n",lCmd);
  86.    system(lCmd);
  87. }
  88.  
  89.  
  90. Instructions()
  91. {
  92.    puts("\a")
  93.    puts(`DoList - Perform command on every element in a list`)
  94.    puts(``)
  95.    puts(`SYNTAX DoList <Command> [ListFile] [/SKIP skip] [/TRUNC trunc]`)
  96.    puts(``)
  97.    puts(`WHERE: Command - Command to perform on each element in list, this may contain`)
  98.    puts(`                 the insert sequence ` INSERT_SEQUENCE ` for list element`)
  99.    puts(`       ListFile - File specification where each line is list element`)
  100.    puts(`       skip - skip this many characters from each line in list`)
  101.    puts(`       trunc - truncate this many character off the end of each item`)
  102.    puts(``)
  103.    puts(`EXAMPLES: DoList PRINT MyList.lst`)
  104.    puts(`          DIR /b *.TXT | DoList "type ` INSERT_SEQUENCE ` >> ALL.TXT"`)
  105.    puts(`          GREP -li NOMBAS * | DoList "attrib +r" /SKIP 5 /TRUNC 1`)
  106. }
  107.