home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / cenvi29.zip / DOLIST.CMD < prev    next >
OS/2 REXX Batch file  |  1994-06-12  |  3KB  |  107 lines

  1. EXTPROC CEnvi
  2. //*************************************************************
  3. //*** DoList.cmd - perform some action on every element     ***
  4. //*** ver.1        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.  
  61.                ActOnLine(Action,line);
  62.             }
  63.          }
  64.       }
  65.    }
  66. }
  67.  
  68. ActOnLine(Action,Line)  // act on line, including INSERT_SEQUENCE
  69. {
  70.    // create statement replacing INSERT_SEQUENCE with line, or putting
  71.    // line at end if no INSERT_SEQUENCE
  72.    strcpy(lCmd,Action);
  73.  
  74.    // If insert sequence then replace with Line, else concatenate line
  75.    if ( insert = strstr(lCmd,INSERT_SEQUENCE) ) {
  76.       strcpy(insert + strlen(Line),insert+strlen(INSERT_SEQUENCE));
  77.       memcpy(insert,Line,strlen(Line));
  78.    } else {
  79.       strcat(lCmd," ");
  80.       strcat(lCmd,Line);
  81.    }
  82.  
  83.    // show and execute the command
  84.    printf("%s\n",lCmd);
  85.    system(lCmd);
  86. }
  87.                // finally, line is text to act upon
  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 - trancate 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.