home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / utils.lbr / PRCOMMEN.CQ / PRCOMMEN.C
Encoding:
Text File  |  1986-05-22  |  9.4 KB  |  360 lines

  1. #
  2. /*
  3.  
  4.               Copyright (C) 1976
  5.                 by the
  6.               Board of Trustees
  7.                 of the
  8.             University of Illinois
  9.  
  10.              All rights reserved
  11.  
  12.  
  13. NAME:
  14.     pr_comment
  15.  
  16. FUNCTION:
  17.     This routine takes care of scanning and printing comments.
  18.  
  19. ALGORITHM:
  20.     1) Decide where the comment should be aligned, and if lines should
  21.        be broken.
  22.     2) If lines should not be broken and filled, just copy up to end of
  23.        comment.
  24.     3) If lines should be filled, then scan thru input_buffer copying
  25.        characters to com_buf.  Remember where the last blank, tab, or
  26.        newline was.  When line is filled, print up to last blank and 
  27.        continue copying.
  28.  
  29. PARAMETERS:
  30.     None
  31.  
  32. RETURNS:
  33.     Nothing
  34.  
  35. GLOBALS:
  36.     combuf =
  37.     s_com
  38.     e_com =
  39.  
  40.     buf_ptr =
  41.     buf_end
  42.  
  43.     bl_line
  44.     col_1
  45.     com_col =
  46.     com_ind
  47.     decl_com_ind
  48.     decl_on_line
  49.     had_eof
  50.     ind_level
  51.     ind_size
  52.     line_no =
  53.     max_col
  54.     out_com =    Count number of comments
  55.     unindent_displace
  56.     use_ff =
  57.  
  58. CALLS:
  59.     count_spaces
  60.     dump_line
  61.     fill_buffer
  62.     printf        (lib)
  63.  
  64. CALLED BY:
  65.     main
  66.  
  67. HISTORY:
  68.     November 1976    D A Willcox of CAC    Initial coding
  69.     12/6/76        D A Willcox of CAC    Modification to handle 
  70.                         UNIX-style comments
  71.  
  72. */
  73.  
  74. /* this routine processes comments.  It makes an attempt to keep comments from
  75.    going over the max line length.  If a line is too long, it moves everything
  76.    from the last blank to the next comment line.  Blanks and tabs from the
  77.    beginning of the input line are removed */
  78.  
  79. #include "indntglo.h";
  80.  
  81.  
  82. pr_comment () {
  83.     int     now_col;
  84.  /* column we are in now */
  85.     int     box_com;
  86.  /* set to true when we are in a "boxed" comment. In that case, the first
  87.     non-blank char should be lined up with the / in /* */
  88.     int     col_1_com;
  89.  /* this comment should not be touched */
  90.     char   *last_bl;
  91.  /* points to the last blank in the output buffer */
  92.     char    achar;
  93.     char   *t_ptr; /* used for movinf string */
  94.     int     unix_comment;
  95.  /* tri-state variable used to decide if it is a unix-style comment. 0 means
  96.     only blanks since /*, 1 means regular style comment, 2 means unix style
  97.     comment */
  98.  
  99.  
  100.     last_bl = 0;           /* no blanks found so far */
  101.     box_com = col_1_com = false;
  102.  /* at first, assume that we are not in a boxed comment or some other comment
  103.     that should not be touched */
  104.     ++out_coms;               /* keep track of number of comments */
  105.     unix_comment = 0;           /* set flag to let us figure out if there is a
  106.                       unix-style comment */
  107.  
  108. /*----------------------------------------------------------*\ 
  109. |   Figure where to align and how to treat the comment
  110. \*----------------------------------------------------------*/
  111.  
  112.     if (col_1) {           /* if comment starts in column 1 it should not
  113.                       be touched */
  114.     col_1_com = box_com = true;
  115.     com_col = 1;
  116.     }
  117.     else {
  118.     if (*buf_ptr == '-')
  119.         box_com = true;    /* a comment with a '-' immediately after the /*
  120.                       is assumed to be a boxed comment */
  121.     if ( /* bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
  122.     /* klg: check only if this line is blank */
  123.     /* 
  124.      * If this (*and previous lines are*) blank,
  125.      * don't put comment way out at left
  126.      */
  127.         com_col = (ind_level - unindent_displace) * ind_size + 1;
  128.         if (com_col <= 1)
  129.         com_col = 2;
  130.     }
  131.     else {
  132.         com_col = (decl_on_line || ind_level == 0 ? decl_com_ind : com_ind);
  133.     }
  134.     }
  135.  
  136.     *e_com++ = '/';           /* put '/*' into buffer */
  137.     *e_com++ = '*';
  138.     if (*buf_ptr != ' ' && !box_com)
  139.     *e_com++ = ' ';
  140.  
  141.     *e_com = '\0';
  142.     now_col = count_spaces (com_col, s_com);
  143.  /* figure where what column we would be in if we printed the comment now */
  144.  
  145.  
  146. /*----------------------------------------------------------*\ 
  147. |    Start to copy the comment
  148. \*----------------------------------------------------------*/
  149.  
  150.     while (1) {               /* this loop will go until the comment is copied 
  151.                    */
  152.     switch (*buf_ptr) {    /* this checks for various spcl cases */
  153.         case 014:            /* check for a form feed */
  154.         if (!box_com) {/* in a text comment, break the line here */
  155.             use_ff = true;
  156.         /* fix so dump_line uses a form feed */
  157.             dump_line ();
  158.             last_bl = 0;
  159.             *e_com++ = ' ';
  160.             *e_com++ = ' ';
  161.             *e_com++ = ' ';
  162.             do {       /* get rid of leading blanks */
  163.             if (++buf_ptr >= buf_end)
  164.                 fill_buffer ();
  165.             } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  166.         }
  167.         else {
  168.             if (++buf_ptr >= buf_end)
  169.             fill_buffer ();
  170.             *e_com++ = 014;
  171.         }
  172.  
  173.         break;
  174.  
  175.         case '\n': 
  176.         if (had_eof) { /* check for unexpected eof */
  177.             printf ("Unterminated comment\n");
  178.             *e_com = '\0';
  179.             dump_line ();
  180.             return;
  181.         }
  182.  
  183.         if (box_com) { /* if this is a boxed comment, we don't ignore
  184.                       the newline */
  185.             *e_com = '\0';
  186.             dump_line ();
  187.             ++line_no;
  188.             now_col = com_col;
  189.  
  190.             if (!col_1_com) {
  191.             /* if merely a boxed comment, we should line up first
  192.                non-blank character */
  193.             do {   /* flush leading non-blanks */
  194.                 if (++buf_ptr >= buf_end)
  195.                 fill_buffer ();
  196.             } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  197.             }
  198.             else {     /* make sure we at least flush the blank */
  199.             if (++buf_ptr >= buf_end)
  200.                 fill_buffer ();
  201.             }
  202.  
  203.             break;
  204.         }
  205.  
  206.         if (unix_comment != 1) {
  207.         /* we are in unix_style comment */
  208.             if (unix_comment == 0 && s_code == e_code) {
  209.             /* if it is a UNIX-style comment, ignore the requirement
  210.                that pervious line be blank for unindention */
  211.             com_col = (ind_level - unindent_displace) * ind_size + 1;
  212.             if (com_col <= 1)
  213.                 com_col = 2;
  214.             }
  215.  
  216.             unix_comment = 2;
  217.         /* permanently remember that we are in this type of comment */
  218.             dump_line ();
  219.             ++line_no;
  220.             now_col = com_col;
  221.             *e_com++ = ' ';
  222.         /* fix so that the star at the start of the line will line up 
  223.         */
  224.             do           /* flush leading white space */
  225.             if (++buf_ptr >= buf_end)
  226.                 fill_buffer ();
  227.             while (*buf_ptr == ' ' || *buf_ptr == '\t');
  228.             break;
  229.         }
  230.  
  231.         if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
  232.             last_bl = e_com - 1;
  233.         /* if there was a space at the end of the last line, remember where
  234.            it was */
  235.         else {           /* otherwise, insert one */
  236.             last_bl = e_com;
  237.             *e_com++ = ' ';
  238.             ++now_col;
  239.         }
  240.  
  241.         ++line_no;     /* keep track of input line number */
  242.         do {           /* copy any blanks and/or tabs at start of next
  243.                       line */
  244.             if (++buf_ptr >= buf_end)
  245.             fill_buffer ();
  246.         } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  247.  
  248.         break;           /* end of case for newline */
  249.  
  250.         case '*':            /* must check for possibility of being at end of
  251.                       comment */
  252.         if (++buf_ptr >= buf_end)
  253.                    /* get to next char after * */
  254.             fill_buffer ();
  255.  
  256.         if (unix_comment == 0)
  257.                    /* set flag to show we are not in unix-style
  258.                       comment */
  259.             unix_comment = 1;
  260.  
  261.         if (*buf_ptr == '/') {
  262.         /* it is the end!!! */
  263.             if (++buf_ptr >= buf_end)
  264.             fill_buffer ();
  265.  
  266.             if (*(e_com - 1) != ' ' && !box_com) {
  267.             /* insure blank before end */
  268.             *e_com++ = ' ';
  269.             ++now_col;
  270.             }
  271.  
  272.             if (now_col > max_col - 2 && !box_com) {
  273.             /* check if star-slash will go over line */
  274.             *e_com = '\0';
  275.             /* it will */
  276.             dump_line ();
  277.             now_col = com_col;
  278.             }
  279.  
  280.             *e_com++ = '*';
  281.         /* move end of comment */
  282.             *e_com++ = '/';
  283.             *e_com = '\0';
  284.             return;    /* we is done */
  285.         }           /* end of end of comment */
  286.  
  287.  
  288.         else {           /* handle isolated '*' */
  289.             *e_com++ = '*';
  290.             ++now_col;
  291.             break;
  292.         }
  293.         /* end of processing of * */
  294.  
  295.         default:            /* we have a random char */
  296.         if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
  297.             unix_comment = 1;
  298.         /* we are not in unix-style comment */
  299.  
  300.         *e_com = *buf_ptr++;
  301.         if (buf_ptr >= buf_end)
  302.             fill_buffer ();
  303.  
  304.         if (*e_com == '\t')
  305.                    /* keep track of column */
  306.             now_col = ((now_col - 1) & tabmask) + tabsize + 1;
  307.         else
  308.             if (*e_com == '')
  309.                    /* this is a backspace */
  310.             --now_col;
  311.             else
  312.             ++now_col;
  313.  
  314.         if (*e_com == ' ' || *e_com == '\t')
  315.             last_bl = e_com;
  316.         /* remember we saw a blank */
  317.  
  318.         ++e_com;
  319.         if (now_col > max_col && !box_com && unix_comment == 1) {
  320.         /* the comment is too long, it must be broken up */
  321.             if (last_bl == 0) {
  322.             /* we have seen no blanks */
  323.             printf ("%d: Comment too long\n", line_no);
  324.             last_bl = e_com;
  325.             /* fake it */
  326.             *e_com++ = ' ';
  327.             }
  328.  
  329.             *e_com = '\0';
  330.         /* print what we have */
  331.             *last_bl = '\0';
  332.             e_com = last_bl;
  333.             dump_line ();
  334.  
  335.             *e_com++ = ' ';
  336.         /* add blanks for continuation */
  337.             *e_com++ = ' ';
  338.             *e_com++ = ' ';
  339.  
  340.             t_ptr = last_bl + 1;
  341.             last_bl = 0;
  342.             while (*t_ptr != '\0') {
  343.             /* move unprinted pare of comment down in buffer */
  344.             if (*t_ptr == ' ' || *t_ptr == '\t')
  345.                 last_bl = e_com;
  346.             *e_com++ = *t_ptr++;
  347.             }
  348.  
  349.             *e_com = '\0';
  350.             now_col = count_spaces (com_col, s_com);
  351.         /* recompute current position */
  352.         }           /* end of code for splitting a comment */
  353.         break;           /* end of default case */
  354.  
  355.  
  356.     }               /* end of switch */
  357.  
  358.     }                   /* end of while (1) */
  359. }           /* end of pr_comment */
  360.