home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RFMRC.ZIP / REFORMRC.AWK
Text File  |  1991-11-12  |  19KB  |  699 lines

  1. #   REFORMRC.AWK - Script to convert a .RC file into a clearer form
  2. #
  3. #   (c) Copyright 1991 Software Architects, Inc.  All Rights Reserved.
  4. #
  5. #   Written by: Guy Scharf
  6. #               Software Architects, Inc.
  7. #               2163 Jardin Drive
  8. #               Mountain View, CA   94040
  9. #               (415) 948-9186
  10. #               FAX: (415) 948-1620
  11. #               CompuServe 76702,557
  12. #   
  13. #   Changes generic CONTROL statements to more specific RC statements
  14. #
  15. #   This program reformats a .RC or .DLG file as produced by the dialog
  16. #   box editor or CASE:PM.  It is dependent on the line formatting used
  17. #   by these tools.  It will not reformat hand-written .RC files with
  18. #   different line ending conventions.  It cannot be run recursively.
  19. #
  20. #   As an awk script, this program is simply a line reformatted.  It does
  21. #   not contain a .RC language syntax scanner.
  22. #
  23. #   Permission is granted to use this script and to modify it.  Software
  24. #   Architects, Inc. accepts no responsibility for the result.  I.e.,
  25. #   this software is supplied "AS IS" and you bear all the responsibility
  26. #   of making sure it does what you want.
  27. #
  28. #   We would appreciate your sending us any changes that improve the
  29. #   program.
  30. #
  31. #   NOTE: REFORMRC.AWK is an internal Software Architects, Inc. tool and
  32. #         is an envolving work in progress. It is not complete nor does
  33. #         it handle all situations.  It is supposed to pass through things
  34. #         it does not recognized unscathed, but this is not guaranteed.
  35. #         I.e., Keep a backup of your .RC file!
  36. #
  37. #         I expect someday to rewrite this as a more intelligent REXX program.
  38. #
  39. #   Usage:
  40. #
  41. #       awk -f reformrc.awk program.rc >newprog.rc
  42. #
  43. #       After running awk, search for "***" in the output file, which
  44. #       will mark any errors.  Program may get confused if there are
  45. #       commas in any text strings.  
  46. #
  47. #       This program has been tested with PolyAWK and MKS AWK.
  48. #
  49. #   History:
  50. #       07/31/91: o Go back to PUSHBUTTON statements and use NOT clauses
  51. #                   to eliminate unwanted defaults.
  52. #       06/23/91: o Changed COMBOBOX to new format (disagrees with IBM
  53. #                   PT&I documentation, but it works, and APARs have
  54. #                   been issued against documentation).
  55. #                 o Don't convert ICON control statement.  It does not
  56. #                   work reliably, and IBM and MS disagree on format.
  57. #                 o Revise PUSHBUTTON handling to better match CUA.
  58. #                   Leave them as CONTROL statements, set WS_TABSTOP
  59. #                   on the first one, but not on any others.  Also
  60. #                   set WS_GROUP on first one.
  61. #                 o Format CONTROL statements that do not get changed
  62. #                   into another statement.
  63. #
  64. ############################
  65.  
  66. #
  67. #   Look for MENU statements and try to line them up
  68. #
  69. / MENUITEM | SUBMENU | HELPITEM | HELPSUBITEM / {
  70.  
  71.     if ($1 == "MENUITEM" || $1 == "SUBMENU" || $1 == "HELPITEM" || $1 == "HELPSUBITEM")
  72.     {
  73.         indval = index($0,$1)
  74.         indspl = index(substr($0,indval), " ")
  75.  
  76.         nfields = split (substr($0,indval+indspl), arr, ",")
  77.         
  78.         text = trim(arr[1])
  79.         id = trim(arr[2])
  80.         type = trim(arr[3])
  81.         extra = ""
  82.         for (i=4; i <=nfields; i++)
  83.         {
  84.             if (extra != "")
  85.                 extra = extra + ", "
  86.             extra = extra trim(arr[i])
  87.         }
  88.         indval = index($0,$1)
  89.         outline = ""
  90.         for (i=1; i<=indval; i++)
  91.             outline = outline " "
  92.         outline = outline $1 "  " text
  93.         if (text == "SEPARATOR")
  94.         {
  95.             print outline
  96.             next
  97.         }
  98.         outline = outline ", "
  99.         if ($1 == "MENUITEM")
  100.             indset = indval+35
  101.         else if ($1 == "HELPSUBITEM")
  102.             indset = indval+40
  103.         else
  104.             indset = indval+20
  105.         while (length(outline) > indset)
  106.             indset += 5
  107.         while (length(outline)<indset)
  108.             outline = outline " "
  109.  
  110.         outline = outline id
  111.         if (type != "")
  112.             outline = outline ", "
  113.  
  114.         while (length(outline)<(indset+15))
  115.             outline = outline " "
  116.         outline = outline type
  117.         if (extra != "")
  118.             outline = outline ", " extra
  119.         print outline
  120.         next
  121.     }
  122.         }
  123.  
  124. #
  125. #   Mark beginning of new dialog template
  126. #
  127. /^DLGTEMPLATE/  {
  128.                     firstpush = 1
  129.                 }
  130.  
  131.  
  132. #
  133. #       Look for CONTROL statements
  134. #
  135.  
  136. {
  137.     if (NF == 0)
  138.     {
  139.         print $0
  140.         next
  141.     }
  142.  
  143.     if ($1 != "CONTROL")
  144.     {
  145.         print $0
  146.         next
  147.     }
  148.  
  149.     ctl = $0
  150.  
  151. #
  152. #   Read in all lines of the CONTROL statement, concatenating them
  153. #
  154.     while (substr(ctl,length(ctl),1) == " ")
  155.     {
  156.         ctl = substr(ctl,1, length(ctl)-1)
  157.     }
  158.  
  159.     while ((substr(ctl,length(ctl),1) == ",") || (substr(ctl,length(ctl),1) == "|") )
  160.     {
  161.         getline
  162.         ctl = ctl $0
  163.         while (substr(ctl,length(ctl),1) == " ")
  164.         {
  165.             ctl = substr(ctl,1, length(ctl)-1)
  166.         }
  167.     }
  168.  
  169. #
  170. #   Parse the CONTROL statement
  171. #
  172.     nfields = split (ctl, arr, ",")
  173.  
  174.     if (nfields != 8)
  175.     {
  176.         print "*** Wrong number of fields found, got "  nfields
  177.         print ctl
  178.         next
  179.     }
  180.  
  181.     startfmt = "%s%-11s "
  182.  
  183.     i = index(arr[1], "CONTROL ")
  184.     blankhdr = substr(arr[1], 1, i-1)
  185.     arr[1] = substr(arr[1], i+8)
  186.  
  187.     for (i = 1; i <= 8; i++)
  188.     {
  189.         while (substr(arr[i],1,1) == " ")
  190.             arr[i] = substr(arr[i],2)
  191.         while (substr(arr[i],length(arr[i]),1) == " ")
  192.             arr[i] = substr(arr[i],1,length(arr[i])-1)
  193.     }
  194.     textstr = arr[1]
  195.     id      = arr[2]
  196.     x       = arr[3]
  197.     y       = arr[4]
  198.     cx      = arr[5]
  199.     cy      = arr[6]
  200.     class   = arr[7]
  201.     style   = arr[8]
  202.  
  203. #
  204. #   Split the control styles up into an array
  205. #
  206.     nstyles = split (style, astyle, "|")
  207.     for (i = 1; i <= nstyles; i++)
  208.     {
  209.         while (substr(astyle[i],1,1) == " ")
  210.             astyle[i] = substr(astyle[i],2)
  211.         while (substr(astyle[i],length(astyle[i]),1) == " ")
  212.             astyle[i] = substr(astyle[i],1,length(astyle[i])-1)
  213.     }
  214.  
  215. #   print "textstr   = " textstr
  216. #   print "id        = " id
  217. #   print "x,y,cx,cy = " x ", " y ", " cx ", " cy
  218. #   print "class     = " class
  219. #   print "style     = " style
  220.  
  221. #
  222. #   Examine each CONTROL type and construct fields to build
  223. #   the new .RC statement for the specific control class
  224. #   identified.
  225. #
  226.     doclass = 0
  227.  
  228.     if ((index(class, "WC_BUTTON") > 0) && (index(style, "BS_PUSHBUTTON") > 0))
  229.     {
  230.         if (index(style, "BS_DEFAULT") > 0)
  231.         {
  232.             key = "DEFPUSHBUTTON"
  233.             if (length(blankhdr) >= 3)
  234.             {
  235.                 blankhdr = substr(blankhdr,4)
  236.                 startfmt = "%s%-14s "
  237.             }
  238.         }
  239.         else
  240.             key = "PUSHBUTTON"
  241.  
  242. #       key = "CONTROL"
  243. #
  244.         newstyle = ""
  245.  
  246.         for (i=1; i <=nstyles; i++)
  247.         {
  248.             if (index(astyle[i], "BS_PUSHBUTTON") >= 1)
  249.                 continue
  250.             if (index(astyle[i], "BS_DEFAULT") >= 1)
  251.                 continue
  252.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  253.                 continue
  254.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  255.                 continue
  256.             if (index(astyle[i], "WS_GROUP") >= 1)
  257.                 continue
  258.  
  259.             if (newstyle != "")
  260.                 newstyle = newstyle " | "
  261.             newstyle = newstyle astyle[i]
  262.         }
  263.  
  264.         if (firstpush)
  265.         {
  266.             firstpush = 0
  267.             if (newstyle != "")
  268.                 newstyle = newstyle " | WS_GROUP "
  269.             else
  270.                 newstyle = " WS_GROUP "
  271.         }
  272.         else
  273.             if (newstyle != "")
  274.                 newstyle = newstyle " | NOT WS_TABSTOP "
  275.             else
  276.                 newstyle = " NOT WS_TABSTOP "
  277.  
  278.         dotext = 1
  279. #       doclass = 1
  280.  
  281.         emit()
  282.     }
  283.     else if ((index(class, "WC_BUTTON") > 0) && (index(style, "BS_AUTOCHECKBOX") > 0))
  284.     {
  285.         key = "AUTOCHECKBOX"
  286.  
  287.         newstyle = ""
  288.  
  289.         for (i=1; i <=nstyles; i++)
  290.         {
  291.             if (index(astyle[i], "BS_AUTOCHECKBOX") >= 1)
  292.                 continue
  293.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  294.                 continue
  295.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  296.                 continue
  297.  
  298.             if (newstyle != "")
  299.                 newstyle = newstyle " | "
  300.             newstyle = newstyle astyle[i]
  301.         }
  302.  
  303.         dotext = 1
  304.  
  305.         emit()
  306.     }
  307.     else if ((index(class, "WC_BUTTON") > 0) && (index(style, "BS_CHECKBOX") > 0))
  308.     {
  309.         key = "CHECKBOX"
  310.  
  311.         newstyle = ""
  312.  
  313.         for (i=1; i <=nstyles; i++)
  314.         {
  315.             if (index(astyle[i], "BS_CHECKBOX") >= 1)
  316.                 continue
  317.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  318.                 continue
  319.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  320.                 continue
  321.  
  322.             if (newstyle != "")
  323.                 newstyle = newstyle " | "
  324.             newstyle = newstyle astyle[i]
  325.         }
  326.  
  327.         dotext = 1
  328.  
  329.         emit()
  330.     }
  331.     else if ((index(class, "WC_STATIC") > 0) && (index(style, "DT_LEFT") > 0))
  332.     {
  333.         key = "LTEXT"
  334.  
  335.         newstyle = ""
  336.  
  337.         for (i=1; i <=nstyles; i++)
  338.         {
  339.             if (index(astyle[i], "DT_LEFT") >= 1)
  340.                 continue                 
  341.             if (index(astyle[i], "SS_TEXT") >= 1)
  342.                 continue                 
  343.             if (index(astyle[i], "WS_GROUP") >= 1)
  344.                 continue                 
  345.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  346.                 continue
  347.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  348.                 continue
  349.  
  350.             if (newstyle != "")
  351.                 newstyle = newstyle " | "
  352.             newstyle = newstyle astyle[i]
  353.         }
  354.  
  355.         dotext = 1
  356.  
  357.         emit()
  358.     }
  359.     else if ((index(class, "WC_STATIC") > 0) && (index(style, "DT_RIGHT") > 0))
  360.     {
  361.         key = "RTEXT"
  362.  
  363.         newstyle = ""
  364.  
  365.         for (i=1; i <=nstyles; i++)
  366.         {
  367.             if (index(astyle[i], "DT_RIGHT") >= 1)
  368.                 continue                 
  369.             if (index(astyle[i], "SS_TEXT") >= 1)
  370.                 continue                 
  371.             if (index(astyle[i], "WS_GROUP") >= 1)
  372.                 continue                 
  373.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  374.                 continue
  375.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  376.                 continue
  377.  
  378.             if (newstyle != "")
  379.                 newstyle = newstyle " | "
  380.             newstyle = newstyle astyle[i]
  381.         }
  382.  
  383.         dotext = 1
  384.  
  385.         emit()
  386.     }
  387.     else if ((index(class, "WC_STATIC") > 0) && (index(style, "DT_CENTER") > 0))
  388.     {
  389.         key = "CTEXT"
  390.  
  391.         newstyle = ""
  392.  
  393.         for (i=1; i <=nstyles; i++)
  394.         {
  395.             if (index(astyle[i], "DT_CENTER") >= 1)
  396.                 continue                 
  397.             if (index(astyle[i], "SS_TEXT") >= 1)
  398.                 continue                 
  399.             if (index(astyle[i], "WS_GROUP") >= 1)
  400.                 continue                 
  401.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  402.                 continue
  403.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  404.                 continue
  405.  
  406.             if (newstyle != "")
  407.                 newstyle = newstyle " | "
  408.             newstyle = newstyle astyle[i]
  409.         }
  410.  
  411.         dotext = 1
  412.  
  413.         emit()
  414.     }
  415.     else if ((index(class, "WC_STATIC") > 0) && (index(style, "SS_GROUPBOX") > 0))
  416.     {
  417.         key = "GROUPBOX"
  418.  
  419.         newstyle = ""
  420.  
  421.         for (i=1; i <=nstyles; i++)
  422.         {
  423.             if (index(astyle[i], "SS_GROUPBOX") >= 1)
  424.                 continue                 
  425.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  426.                 continue
  427.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  428.                 continue
  429.  
  430.             if (newstyle != "")
  431.                 newstyle = newstyle " | "
  432.             newstyle = newstyle astyle[i]
  433.         }
  434.  
  435.         dotext = 1
  436.  
  437.         emit()
  438.     }
  439.     else if ((index(class, "WC_STATIC") > 0) && (index(style, "SS_ICON") > 0))
  440.     {
  441. #        key = "ICON"
  442.         key = "CONTROL"
  443.  
  444.         newstyle = ""
  445.  
  446.         for (i=1; i <=nstyles; i++)
  447.         {
  448.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  449.                 continue
  450.  
  451.             if (newstyle != "")
  452.                 newstyle = newstyle " | "
  453.             newstyle = newstyle astyle[i]
  454.         }
  455.  
  456.         dotext = 1
  457.         doclass = 1
  458.  
  459.         emit()
  460.     }
  461.     else if (index(class, "WC_LISTBOX") > 0) 
  462.     {
  463.         key = "LISTBOX"
  464.  
  465.         newstyle = ""
  466.  
  467.         for (i=1; i <=nstyles; i++)
  468.         {
  469.             if (index(astyle[i], "WS_GROUP") >= 1)
  470.                 continue                 
  471.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  472.                 continue
  473.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  474.                 continue
  475.  
  476.             if (newstyle != "")
  477.                 newstyle = newstyle " | "
  478.             newstyle = newstyle astyle[i]
  479.         }
  480.  
  481.         dotext = 0
  482.  
  483.         emit()
  484.     }
  485.     else if (index(class, "WC_ENTRYFIELD") > 0) 
  486.     {
  487.         key = "ENTRYFIELD"
  488.  
  489.         newstyle = ""
  490.  
  491.         for (i=1; i <=nstyles; i++)
  492.         {
  493.             if (index(astyle[i], "WS_GROUP") >= 1)
  494.                 continue                 
  495.             if (index(astyle[i], "ES_AUTOSCROLL") >= 1)
  496.                 continue                 
  497.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  498.                 continue
  499.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  500.                 continue
  501.  
  502.             if (newstyle != "")
  503.                 newstyle = newstyle " | "
  504.             newstyle = newstyle astyle[i]
  505.         }
  506.  
  507.         dotext = 1
  508.  
  509.         emit()
  510.     }
  511.     else if (index(class, "WC_COMBOBOX") > 0) 
  512.     {
  513.         key = "COMBOBOX"
  514.  
  515.         newstyle = ""
  516.  
  517.         for (i=1; i <=nstyles; i++)
  518.         {
  519.             if (index(astyle[i], "WS_GROUP") >= 1)
  520.                 continue                 
  521.             if (index(astyle[i], "CBS_SIMPLE") >= 1)
  522.                 continue                 
  523.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  524.                 continue
  525.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  526.                 continue
  527.  
  528.             if (newstyle != "")
  529.                 newstyle = newstyle " | "
  530.             newstyle = newstyle astyle[i]
  531.         }
  532.  
  533.         dotext = 1
  534.  
  535.         emit()
  536.     }
  537.     else if ((index(class, "WC_BUTTON") > 0) && (index(style, "BS_AUTORADIOBUTTON") > 0))
  538.     {
  539.         key = "AUTORADIOBUTTON"
  540.  
  541.         newstyle = ""
  542.  
  543.         for (i=1; i <=nstyles; i++)
  544.         {
  545.             if (index(astyle[i], "BS_AUTORADIOBUTTON") >= 1)
  546.                 continue                 
  547.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  548.                 continue
  549.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  550.                 continue
  551.  
  552.             if (newstyle != "")
  553.                 newstyle = newstyle " | "
  554.             newstyle = newstyle astyle[i]
  555.         }
  556.  
  557.         dotext = 1
  558.  
  559.         emit()
  560.     }
  561.     else if ((index(class, "WC_BUTTON") > 0) && (index(style, "BS_RADIOBUTTON") > 0))
  562.     {
  563.         key = "RADIOBUTTON"
  564.  
  565.         newstyle = ""
  566.  
  567.         for (i=1; i <=nstyles; i++)
  568.         {
  569.             if (index(astyle[i], "BS_RADIOBUTTON") >= 1)
  570.                 continue                 
  571.             if (index(astyle[i], "WS_TABSTOP") >= 1)
  572.                 continue
  573.             if (index(astyle[i], "WS_VISIBLE") >= 1)
  574.                 continue
  575.  
  576.             if (newstyle != "")
  577.                 newstyle = newstyle " | "
  578.             newstyle = newstyle astyle[i]
  579.         }
  580.  
  581.         dotext = 1
  582.  
  583.         emit()
  584.     }
  585.  
  586.     else
  587.     {
  588.         key = "CONTROL"
  589.  
  590.         newstyle = ""
  591.  
  592.         for (i=1; i <=nstyles; i++)
  593.         {
  594.             if (newstyle != "")
  595.                 newstyle = newstyle " | "
  596.             newstyle = newstyle astyle[i]
  597.         }
  598.  
  599.         dotext = 1
  600.         doclass = 1
  601.  
  602.         emit()
  603.     }
  604. }
  605.  
  606.  
  607. #
  608. #   EMIT creates the output statement from parsed and evaluated input
  609. #
  610. function emit()
  611. {
  612.     outline = sprintf(startfmt, blankhdr, key)
  613.  
  614.     if (dotext == 1)
  615.     {
  616.         outline = outline sprintf("%s, ", textstr)
  617.     }
  618.  
  619.     indval = length(outline)
  620.     while (indval<35)
  621.     {
  622.         outline = outline " "
  623.         indval++
  624.     }
  625.     outline = outline sprintf("%s, ", id)
  626.  
  627.     indval = length(outline)
  628.  
  629.     if (indval > 45)
  630.     {
  631.         print outline
  632.         outline = "                                        "
  633.         indval = length(outline)
  634.     }
  635.     while (indval<45)
  636.     {
  637.         outline = outline " "
  638.         indval++
  639.     }
  640.  
  641.     outline = outline sprintf("%4s, %4s, %4s, %4s", trim(x), trim(y), trim(cx), trim(cy))
  642.     if (doclass)
  643.     {
  644.         outline = outline sprintf(", %s", class)
  645.     }
  646.  
  647.     if (newstyle != "")
  648.     {
  649.         outline = outline ", "
  650.         firststyle = 1
  651.  
  652.         nstyles = split (newstyle, astyle, "|")
  653.  
  654.         for (i=1; i <=nstyles; i++)
  655.         {
  656.             if (firststyle == 1)
  657.                 firststyle = 0
  658.             else
  659.                 outline = outline " |"
  660.  
  661.             if (length(outline) > 75)
  662.             {
  663.                 printf("%s\n", outline)
  664.                 outline = "                   "
  665.             }
  666.  
  667.             if (substr(astyle[i],1,1) == " ")
  668.                 astyle[i] = substr(astyle[i],2)
  669.  
  670.             outline = outline " " astyle[i]
  671.         }
  672.     }
  673.  
  674.     if (length(outline) > 0)
  675.         printf("%s\n",outline)
  676.  
  677. #   printf(startfmt, blankhdr, key)
  678. #
  679. #   if (dotext == 1)
  680. #       printf("%s, ", textstr)
  681. #
  682. #   printf("%s, %s, %s, %s, %s", id, x, y, cx, cy)
  683. #
  684. #   if (newstyle != "")
  685. #       printf(", %s", newstyle)
  686. #   printf("\n")
  687.     return 
  688. }
  689.  
  690. function trim(val)
  691. {
  692.     while (substr(val,1,1) == " ")
  693.         val = substr(val,2)
  694.     while (substr(val,length(val),1) == " ")
  695.         val = substr(val,length(val)-1)
  696.     return val
  697. }
  698. 
  699.