home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / comnt110.zip / Comment.Cmd next >
OS/2 REXX Batch file  |  1996-04-04  |  10KB  |  282 lines

  1. /***************************************************************************
  2.  * Comment.Cmd 1.10 (4-Apr-1996 22:55:10)
  3.  *
  4.  * Copyright 1996 Christopher J. Madsen
  5.  *
  6.  * Comment.Cmd is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * Comment.Cmd is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * Christopher J. Madsen        <ac608@yfn.ysu.edu>
  21.  * 3222 Darby Ln.
  22.  * Denton, TX  76207-1305
  23.  *
  24.  * Display or change file comments
  25.  ***************************************************************************/
  26. '@echo off'
  27.  
  28. IF RxFuncQuery('SysLoadFuncs') THEN DO
  29.   CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  30.   CALL SysLoadFuncs
  31. END
  32.  
  33. PARSE SOURCE . . commandPath
  34. commandName = filespec('n',commandPath)
  35.  
  36. versionStr = 'Comment.Cmd version 1.10 (4-Apr-1996)'
  37.  
  38. /***************************************************************************
  39.  * Parse options:
  40.  ***************************************************************************/
  41. PARSE ARG commandLine
  42.  
  43. longOpts = '?:HELP: A:ALL-FILES: D:DELETE: F:FORCE: Q:QUIET: S:SUBJECT:'
  44. PARSE VALUE rxGetOpt(commandLine,'',longOpts) WITH resultCode commandLine
  45.  
  46. IF resultCode <> 0 THEN DO
  47.   SAY commandName': ' commandLine
  48.   SIGNAL Usage
  49. END
  50.  
  51. allFiles    = 0                 /* Do not list files without comments */
  52. deleting    = 0                 /* We are not deleting comments */
  53. eaHeader    = 'FDFF'x           /* Length preceding ASCII */
  54. eaHdrLen    = LENGTH(eaHeader)  /* Remember length of EA header */
  55. eaDesc      = 'comments'        /* The EA name to use in messages */
  56. eaName      = '.comments'       /* The EA we are manipulating */
  57. forceUpdate = 0                 /* Don't replace existing comments */
  58. verbose     = 1                 /* Display progress messages */
  59.  
  60. globalVars = 'allFiles eaHdrLen eaHeader eaDesc eaName forceUpdate verbose'
  61.  
  62. DO FOREVER
  63.   PARSE VAR commandLine argument commandLine
  64.   SELECT
  65.     WHEN argument = '--' THEN LEAVE
  66.     WHEN argument = '-A' THEN
  67.       allFiles = 1              /* List all files */
  68.     WHEN argument = '-D' THEN
  69.       deleting = 1              /* Delete comments from files */
  70.     WHEN argument = '-F' THEN
  71.       forceUpdate = 1           /* Replace existing comments */
  72.     WHEN argument = '-Q' THEN
  73.       verbose = 0               /* Don't display progress messages */
  74.     WHEN argument = '-S' THEN DO
  75.       /* Work with the subject attribute */
  76.       eaDesc = 'subjects'
  77.       eaName = '.subject'
  78.     END /* when --subject */
  79.     WHEN argument = '--LICENSE' THEN
  80.       SIGNAL License
  81.     WHEN argument = '--VERSION' THEN DO
  82.       SAY versionStr
  83.       EXIT
  84.     END /* when --version */
  85.     OTHERWISE
  86.       IF argument <> '-?' THEN
  87.         SAY commandName': Unrecognized option `'argument"'"
  88.       SIGNAL Usage
  89.   END /* select */
  90. END /* do while more options */
  91.  
  92. /***************************************************************************
  93.  * Parse filenames and perform action:
  94.  ***************************************************************************/
  95. fileSpec = nextArg()
  96.  
  97. IF fileSpec = '' THEN SIGNAL Usage
  98.  
  99. IF deleting THEN DO
  100.   IF verbose THEN do
  101.     CALL CHAROUT ,,
  102.       'Are you sure you want to delete the' eaDesc 'from those files (Y/N)? '
  103.     key = SysGetKey()
  104.     SAY
  105.     IF key <> 'Y' & key <> 'y' THEN EXIT 1
  106.     SAY 'Deleting file' eaDesc'...'
  107.   END /* if verbose */
  108.   DO WHILE fileSpec <> ''
  109.     IF SysFileTree(fileSpec, 'file', 'O') THEN SIGNAL ERROR
  110.     CALL deleteComments
  111.     fileSpec = nextArg()
  112.   END /* DO */
  113. END /* if deleting */
  114. ELSE DO
  115.   comment = commandLine
  116.  
  117.   IF SysFileTree(fileSpec, 'file', 'O') THEN SIGNAL ERROR
  118.  
  119.   IF file.0 = 0 THEN DO
  120.     IF verbose THEN SAY 'No files.'
  121.     EXIT
  122.   END
  123.  
  124.   IF comment = '' THEN
  125.     CALL displayComments
  126.   ELSE
  127.     CALL setComments comment
  128. END /* else change or list comments */
  129.  
  130. EXIT /* main program */
  131.  
  132. /***************************************************************************
  133.  * Delete file comments
  134.  *
  135.  * The stem variable FILE should contain a list of complete pathnames.
  136.  * File.0 should contain the number of files.
  137.  ***************************************************************************/
  138. deleteComments: PROCEDURE EXPOSE file. (globalVars)
  139.   DO i = 1 TO file.0
  140.     fileName = filespec('n',file.i)
  141.     IF LENGTH(fileName) < 12 THEN fileName = RIGHT(fileName,12)
  142.     oldComment = readComment(file.i)
  143.     IF oldComment <> '' THEN DO
  144.       IF SysPutEA(file.i,eaName,'') = 0 & verbose THEN
  145.           SAY fileName': Deleted "'oldComment'"'
  146.       ELSE
  147.         SAY fileName': Error'
  148.     END /* if file has comment */
  149.   END /* for i */
  150.   RETURN
  151. /* end deleteComments */
  152.  
  153. /***************************************************************************
  154.  * Display file comments
  155.  *
  156.  * The stem variable FILE should contain a list of complete pathnames.
  157.  * File.0 should contain the number of files.
  158.  ***************************************************************************/
  159. displayComments: PROCEDURE EXPOSE file. (globalVars)
  160.   DO i = 1 TO file.0
  161.     comment = readComment(file.i)
  162.     IF (comment <> '') | allFiles THEN DO
  163.       fileName = filespec('n',file.i)
  164.       IF LENGTH(fileName) < 12 THEN
  165.         fileName = RIGHT(fileName,12)
  166.       SAY fileName':' comment
  167.     END /* if */
  168.   END /* for i */
  169.   RETURN
  170. /* end displayComments */
  171.  
  172. /***************************************************************************
  173.  * Set file comments
  174.  *
  175.  * The stem variable FILE should contain a list of complete pathnames.
  176.  * File.0 should contain the number of files.
  177.  ***************************************************************************/
  178. setComments: PROCEDURE EXPOSE file. (globalVars)
  179.   PARSE ARG comment
  180.  
  181.   fileComment = eaHeader||D2C(LENGTH(comment))||'00'x||comment
  182.   IF verbose THEN
  183.     SAY 'Setting file' eaDesc 'to "'comment'"...'
  184.   DO i = 1 TO file.0
  185.     fileName = filespec('n',file.i)
  186.     IF LENGTH(fileName) < 12 THEN fileName = RIGHT(fileName,12)
  187.     oldComment = readComment(file.i)
  188.     IF forceUpdate | oldComment = '' THEN DO
  189.       IF SysPutEA(file.i,eaName, fileComment) = 0 & verbose THEN DO
  190.         IF oldComment = '' THEN
  191.           SAY fileName': OK'
  192.         ELSE
  193.           SAY fileName': OK (Replaced "'oldComment'")'
  194.       END /* if verbose */
  195.       ELSE
  196.         SAY fileName': Error'
  197.     END /* if we should write comment to this file */
  198.     ELSE
  199.       IF verbose THEN SAY fileName': Did not replace "'oldComment'"'
  200.   END /* for i */
  201.   RETURN
  202. /* end setComments */
  203.  
  204. /***************************************************************************
  205.  * Read file comment
  206.  *
  207.  * Input:
  208.  *   pathname of file to get comment from
  209.  * Output:
  210.  *   The comment.  (Null string if no comment)
  211.  ***************************************************************************/
  212. readComment: PROCEDURE EXPOSE (globalVars)
  213.   PARSE ARG fileName
  214.   IF SysGetEA(fileName, eaName, 'comment') <> 0 THEN
  215.     RETURN ''
  216.   IF comment = '' THEN
  217.     RETURN ''
  218.   IF LEFT(comment,eaHdrLen) = eaHeader THEN
  219.     RETURN SUBSTR(comment,eaHdrLen+3)
  220.   IF LEFT(comment,8) = 'DFFF00000100FDFF'x THEN
  221.     RETURN SUBSTR(comment,11) /* Multi-Value Multi-Type LP ASCII */
  222.   ELSE
  223.     RETURN '<Multi-line comment>'
  224. /* end readComment */
  225.  
  226. /***************************************************************************
  227.  * Read an argument from the command line:
  228.  *
  229.  * Input:
  230.  *   The command line must be stored in commandLine
  231.  * Output:
  232.  *   Returns the argument.  If it was enclosed in quotation marks, the
  233.  *     quotes are removed.
  234.  *   The retrieved argument is removed from commandLine.
  235.  ***************************************************************************/
  236. nextArg: PROCEDURE EXPOSE commandLine
  237.   IF POS(LEFT(commandLine,1),'''"') > 0 THEN DO
  238.     /* Get a quoted argument */
  239.     quoteChar = LEFT(commandLine,1)
  240.     PARSE VAR commandLine (quoteChar) argument (quoteChar) commandLine
  241.   END /* if this is a quoted argument */
  242.   ELSE
  243.     PARSE VAR commandLine argument commandLine
  244.   commandLine = STRIP(commandLine,'L') /* Strip leading blanks */
  245.   RETURN argument
  246. /* end nextArg */
  247.  
  248. /***************************************************************************
  249.  * Signal Handlers
  250.  ***************************************************************************/
  251. Error:
  252.   SAY 'Abnormal exit!'
  253.   EXIT 1
  254.  
  255. License:
  256.   SAY
  257.   SAY versionStr
  258.   /* Read license text from top of command file */
  259.   CALL LINEIN commandPath, 1, 0
  260.   DO  3; CALL LINEIN commandPath;                     END
  261.   DO 19; CALL LINEOUT, SUBSTR(LINEIN(commandPath),4); END
  262.   EXIT
  263.  
  264. Usage:
  265.   SAY
  266.   SAY versionStr
  267.   SAY
  268.   SAY 'Usage: comment [options] fileSpec [comment]'
  269.   SAY '       comment -d fileSpec...'
  270.   SAY 'Options:'
  271.   SAY '    -a, --all-files List all files (even those without comments)'
  272.   SAY '    -d, --delete    Remove comments from specified files'
  273.   SAY '    -f, --force     Replace existing comments'
  274.   SAY '    -q, --quiet     Do not display progress messages',
  275.     'or ask for confirmation'
  276.   SAY '    -s, --subject   Work with the subject field',
  277.     'instead of the comments field'
  278.   SAY '    -?, --help      Display this help and exit'
  279.   SAY '        --license   Display license information and exit'
  280.   SAY '        --version   Display version information and exit'
  281.   EXIT
  282.