home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / src / niceprintf.c < prev    next >
C/C++ Source or Header  |  1999-12-18  |  10KB  |  376 lines

  1. /****************************************************************
  2. Copyright 1990, 1991 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27.  
  28. #define TOO_LONG_INDENT (2 * tab_size)
  29. #define MAX_INDENT 44
  30. #define MIN_INDENT 22
  31. static int last_was_newline = 0;
  32. int indent = 0;
  33. int in_comment = 0;
  34.  extern int gflag1;
  35.  extern char *file_name;
  36.  
  37.  static int
  38. write_indent(fp, use_indent, extra_indent, start, end)
  39.  FILE *fp;
  40.  int use_indent, extra_indent;
  41.  char *start, *end;
  42. {
  43.     int ind, tab;
  44.  
  45.     if (gflag1 && last_was_newline)
  46.     fprintf(fp, "#line %ld \"%s\"\n", lineno, file_name);
  47.     if (last_was_newline && use_indent) {
  48.     if (*start == '\n') do {
  49.         putc('\n', fp);
  50.         if (++start > end)
  51.             return;
  52.         }
  53.         while(*start == '\n');
  54.  
  55.     ind = indent <= MAX_INDENT
  56.         ? indent
  57.         : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  58.  
  59.     tab = ind + extra_indent;
  60.  
  61.     while (tab > 7) {
  62.         putc ('\t', fp);
  63.         tab -= 8;
  64.     } /* while */
  65.  
  66.     while (tab-- > 0)
  67.         putc (' ', fp);
  68.     } /* if last_was_newline */
  69.  
  70.     while (start <= end)
  71. #ifdef _AMIGA
  72.     fputc (*start++, fp);
  73. #else
  74.     putc (*start++, fp);
  75. #endif
  76. } /* write_indent */
  77.  
  78.  
  79. /*VARARGS2*/
  80. int margin_printf (fp, a, b, c, d, e, f, g)
  81. FILE *fp;
  82. char *a;
  83. long b, c, d, e, f, g;
  84. {
  85.     ind_printf (0, fp, a, b, c, d, e, f, g);
  86. } /* margin_printf */
  87.  
  88. /*VARARGS2*/
  89. int nice_printf (fp, a, b, c, d, e, f, g)
  90. FILE *fp;
  91. char *a;
  92. long b, c, d, e, f, g;
  93. {
  94.     ind_printf (1, fp, a, b, c, d, e, f, g);
  95. } /* nice_printf */
  96.  
  97.  
  98. #define  max_line_len c_output_line_length
  99.          /* 74Number of characters allowed on an output
  100.                        line.  This assumes newlines are handled
  101.                        nicely, i.e. a newline after a full text
  102.                        line on a terminal is ignored */
  103.  
  104. /* output_buf   holds the text of the next line to be printed.  It gets
  105.    flushed when a newline is printed.   next_slot   points to the next
  106.    available location in the output buffer, i.e. where the next call to
  107.    nice_printf will have its output stored */
  108.  
  109. static char *output_buf;
  110. static char *next_slot;
  111. static char *string_start;
  112.  
  113. static char *word_start = NULL;
  114. static int cursor_pos = 0;
  115. static int In_string = 0;
  116.  
  117.  void
  118. np_init()
  119. {
  120.     next_slot = output_buf = Alloc(MAX_OUTPUT_SIZE);
  121.     memset(output_buf, 0, MAX_OUTPUT_SIZE);
  122.     }
  123.  
  124.  static char *
  125. adjust_pointer_in_string(pointer)
  126.  register char *pointer;
  127. {
  128.     register char *s, *s1, *se, *s0;
  129.  
  130.     /* arrange not to break \002 */
  131.     s1 = string_start ? string_start : output_buf;
  132.     for(s = s1; s < pointer; s++) {
  133.         s0 = s1;
  134.         s1 = s;
  135.         if (*s == '\\') {
  136.             se = s++ + 4;
  137.             if (se > pointer)
  138.                 break;
  139.             if (*s < '0' || *s > '7')
  140.                 continue;
  141.             while(++s < se)
  142.                 if (*s < '0' || *s > '7')
  143.                     break;
  144.             --s;
  145.             }
  146.         }
  147.     return s0 - 1;
  148.     }
  149.  
  150. /* ANSI says strcpy's behavior is undefined for overlapping args,
  151.  * so we roll our own fwd_strcpy: */
  152.  
  153.  static void
  154. fwd_strcpy(t, s)
  155.  register char *t, *s;
  156. { while(*t++ = *s++); }
  157.  
  158. /* isident -- true iff character could belong to a unit.  C allows
  159.    letters, numbers and underscores in identifiers.  This also doubles as
  160.    a check for numeric constants, since we include the decimal point and
  161.    minus sign.  The minus has to be here, since the constant "10e-2"
  162.    cannot be broken up.  The '.' also prevents structure references from
  163.    being broken, which is a quite acceptable side effect */
  164.  
  165. #define isident(x) (Tr[x] & 1)
  166. #define isntident(x) (!Tr[x])
  167.  
  168. int ind_printf (use_indent, fp, a, b, c, d, e, f, g)
  169. int use_indent;
  170. FILE *fp;
  171. char *a;
  172. long b, c, d, e, f, g;
  173. {
  174.     extern int max_line_len;
  175.     extern FILEP c_file;
  176.     extern char tr_tab[];    /* in output.c */
  177.     register char *Tr = tr_tab;
  178.     int ch, inc, ind;
  179.     static int extra_indent, last_indent, set_cursor = 1;
  180.  
  181.     cursor_pos += indent - last_indent;
  182.     last_indent = indent;
  183.     sprintf (next_slot, a, b, c, d, e, f, g);
  184.  
  185.     if (fp != c_file) {
  186.     fprintf (fp,"%s", next_slot);
  187.     return 1;
  188.     } /* if fp != c_file */
  189.  
  190.     do {
  191.     char *pointer;
  192.  
  193. /* The   for   loop will parse one output line */
  194.  
  195.     if (set_cursor) {
  196.         ind = indent <= MAX_INDENT
  197.             ? indent
  198.             : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  199.         cursor_pos = ind + extra_indent;
  200.         set_cursor = 0;
  201.         }
  202.     if (in_comment)
  203.             for (pointer = next_slot; *pointer && *pointer != '\n' &&
  204.                 cursor_pos <= max_line_len; pointer++)
  205.             cursor_pos++;
  206.     else
  207.           for (pointer = next_slot; *pointer && *pointer != '\n' &&
  208.         cursor_pos <= max_line_len; pointer++) {
  209.  
  210.         /* Update state variables here */
  211.  
  212.         if (In_string) {
  213.         switch(*pointer) {
  214.             case '\\':
  215.                 if (++cursor_pos > max_line_len) {
  216.                     cursor_pos -= 2;
  217.                     --pointer;
  218.                     goto overflow;
  219.                     }
  220.                 ++pointer;
  221.                 break;
  222.             case '"':
  223.                 In_string = 0;
  224.                 word_start = 0;
  225.             }
  226.         }
  227.         else switch (*pointer) {
  228.             case '"':
  229.             if (cursor_pos + 5 > max_line_len) {
  230.                 word_start = 0;
  231.                 --pointer;
  232.                 goto overflow;
  233.                 }
  234.             In_string = 1;
  235.             string_start = word_start = pointer;
  236.                 break;
  237.             case '\'':
  238.             if (pointer[1] == '\\')
  239.                 if ((ch = pointer[2]) >= '0' && ch <= '7')
  240.                     for(inc = 3; pointer[inc] != '\''
  241.                         && ++inc < 5;);
  242.                 else
  243.                     inc = 3;
  244.             else
  245.                 inc = 2;
  246.             /*debug*/ if (pointer[inc] != '\'')
  247.             /*debug*/  fatalstr("Bad character constant %.10s",
  248.                     pointer);
  249.             if ((cursor_pos += inc) > max_line_len) {
  250.                 cursor_pos -= inc;
  251.                 word_start = 0;
  252.                 --pointer;
  253.                 goto overflow;
  254.                 }
  255.             word_start = pointer;
  256.             pointer += inc;
  257.             break;
  258.         case '\t':
  259.             cursor_pos = 8 * ((cursor_pos + 8) / 8) - 1;
  260.             break;
  261.         default: {
  262.  
  263. /* HACK  Assumes that all characters in an atomic C token will be written
  264.    at the same time.  Must check for tokens first, since '-' is considered
  265.    part of an identifier; checking isident first would mean breaking up "->" */
  266.  
  267.             if (!word_start && isident(*(unsigned char *)pointer))
  268.             word_start = pointer;
  269.             else if (word_start && isntident(*(unsigned char *)pointer))
  270.             word_start = NULL;
  271.             break;
  272.         } /* default */
  273.         } /* switch */
  274.         cursor_pos++;
  275.     } /* for pointer = next_slot */
  276.  overflow:
  277.     if (*pointer == '\0') {
  278.  
  279. /* The output line is not complete, so break out and don't output
  280.    anything.  The current line fragment will be stored in the buffer */
  281.  
  282.         next_slot = pointer;
  283.         break;
  284.     } else {
  285.         char last_char;
  286.         int in_string0 = In_string;
  287.  
  288. /* If the line was too long, move   pointer   back to the character before
  289.    the current word.  This allows line breaking on word boundaries.  Make
  290.    sure that 80 character comment lines get broken up somehow.  We assume
  291.    that any non-string 80 character identifier must be in a comment.
  292. */
  293.  
  294.         if (word_start && *pointer != '\n' && word_start > output_buf)
  295.         if (In_string)
  296.             if (string_start && pointer - string_start < 5)
  297.                 pointer = string_start - 1;
  298.             else {
  299.                 pointer = adjust_pointer_in_string(pointer);
  300.                 string_start = 0;
  301.                 }
  302.         else if (word_start == string_start
  303.                 && pointer - string_start >= 5) {
  304.             pointer = adjust_pointer_in_string(next_slot);
  305.             In_string = 1;
  306.             string_start = 0;
  307.             }
  308.         else
  309.             pointer = word_start - 1;
  310.         else if (cursor_pos > max_line_len) {
  311.         extern char *strchr();
  312.         if (In_string) {
  313.             pointer = adjust_pointer_in_string(pointer);
  314.             if (string_start && pointer > string_start)
  315.                 string_start = 0;
  316.             }
  317.         else if (strchr("&*+-/<=>|", *pointer)
  318.             && strchr("!%&*+-/<=>^|", pointer[-1])) {
  319.             pointer -= 2;
  320.             if (strchr("<>", *pointer)) /* <<=, >>= */
  321.                 pointer--;
  322.             }
  323.         else
  324.             pointer--;
  325.         }
  326.         last_char = *pointer;
  327.         write_indent(fp, use_indent, extra_indent, output_buf, pointer);
  328.         next_slot = output_buf;
  329.         if (In_string && !string_start && Ansi == 1 && last_char != '\n')
  330.         *next_slot++ = '"';
  331.         fwd_strcpy(next_slot, pointer + 1);
  332.  
  333. /* insert a line break */
  334.  
  335.         if (last_char == '\n') {
  336.         if (In_string)
  337.             last_was_newline = 0;
  338.         else {
  339.             last_was_newline = 1;
  340.             extra_indent = 0;
  341.             }
  342.         }
  343.         else {
  344.         extra_indent = TOO_LONG_INDENT;
  345.         if (In_string && !string_start) {
  346.             if (Ansi == 1) {
  347.                 fprintf(fp, "\"\n");
  348.                 use_indent = 1;
  349.                 last_was_newline = 1;
  350.                 }
  351.             else {
  352.                 fprintf(fp, "\\\n");
  353.                 last_was_newline = 0;
  354.                 }
  355.             In_string = in_string0;
  356.             }
  357.         else {
  358.             putc ('\n', fp);
  359.             last_was_newline = 1;
  360.             }
  361.         } /* if *pointer != '\n' */
  362.  
  363.         if (In_string && Ansi != 1 && !string_start)
  364.         cursor_pos = 0;
  365.         else
  366.         set_cursor = 1;
  367.  
  368.         string_start = word_start = NULL;
  369.  
  370.     } /* else */
  371.  
  372.     } while (*next_slot);
  373.  
  374.     return 0;
  375. } /* ind_printf */
  376.