home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / niceprintf.c < prev    next >
C/C++ Source or Header  |  1994-02-25  |  11KB  |  438 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1993, 1994 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. #ifndef KR_headers
  28. #include "stdarg.h"
  29. #endif
  30.  
  31. #define TOO_LONG_INDENT (2 * tab_size)
  32. #define MAX_INDENT 44
  33. #define MIN_INDENT 22
  34. static int last_was_newline = 0;
  35. int indent = 0;
  36. int in_comment = 0;
  37. int in_define = 0;
  38.  extern int gflag1;
  39.  extern char *file_name;
  40.  
  41.  static void ind_printf Argdcl((int, FILE*, char*, va_list));
  42.  
  43.  static void
  44. #ifdef KR_headers
  45. write_indent(fp, use_indent, extra_indent, start, end)
  46.     FILE *fp;
  47.     int use_indent;
  48.     int extra_indent;
  49.     char *start;
  50.     char *end;
  51. #else
  52. write_indent(FILE *fp, int use_indent, int extra_indent, char *start, char *end)
  53. #endif
  54. {
  55.     int ind, tab;
  56.  
  57.     if (gflag1 && last_was_newline)
  58.     fprintf(fp, "#line %ld \"%s\"\n", lineno, infname ? infname : file_name);
  59.     if (in_define == 1) {
  60.     in_define = 2;
  61.     use_indent = 0;
  62.     }
  63.     if (last_was_newline && use_indent) {
  64.     if (*start == '\n') do {
  65.         putc('\n', fp);
  66.         if (++start > end)
  67.             return;
  68.         }
  69.         while(*start == '\n');
  70.  
  71.     ind = indent <= MAX_INDENT
  72.         ? indent
  73.         : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  74.  
  75.     tab = ind + extra_indent;
  76.  
  77.     while (tab > 7) {
  78.         putc ('\t', fp);
  79.         tab -= 8;
  80.     } /* while */
  81.  
  82.     while (tab-- > 0)
  83.         putc (' ', fp);
  84.     } /* if last_was_newline */
  85.  
  86.     while (start <= end)
  87.     putc (*start++, fp);
  88. } /* write_indent */
  89.  
  90. #ifdef KR_headers
  91. /*VARARGS2*/
  92.   void
  93.  margin_printf (fp, a, b, c, d, e, f, g)
  94.   FILE *fp;
  95.   char *a;
  96.   long b, c, d, e, f, g;
  97. {
  98.     ind_printf (0, fp, a, b, c, d, e, f, g);
  99. } /* margin_printf */
  100.  
  101. /*VARARGS2*/
  102.   void
  103.  nice_printf (fp, a, b, c, d, e, f, g)
  104.   FILE *fp;
  105.   char *a;
  106.   long b, c, d, e, f, g;
  107. {
  108.     ind_printf (1, fp, a, b, c, d, e, f, g);
  109. } /* nice_printf */
  110. #define SPRINTF(x,a,b,c,d,e,f,g) sprintf(x,a,b,c,d,e,f,g)
  111.  
  112. #else /* if (!defined(KR_HEADERS)) */
  113.  
  114. #define SPRINTF(x,a,b,c,d,e,f,g) vsprintf(x,a,ap)
  115.  
  116.   void
  117.  margin_printf(FILE *fp, char *fmt, ...)
  118. {
  119.     va_list ap;
  120.     va_start(ap,fmt);
  121.     ind_printf(0, fp, fmt, ap);
  122.     va_end(ap);
  123.     }
  124.  
  125.   void
  126.  nice_printf(FILE *fp, char *fmt, ...)
  127. {
  128.     va_list ap;
  129.     va_start(ap,fmt);
  130.     ind_printf(1, fp, fmt, ap);
  131.     va_end(ap);
  132.     }
  133. #endif
  134.  
  135. #define  max_line_len c_output_line_length
  136.          /* 74Number of characters allowed on an output
  137.                        line.  This assumes newlines are handled
  138.                        nicely, i.e. a newline after a full text
  139.                        line on a terminal is ignored */
  140.  
  141. /* output_buf   holds the text of the next line to be printed.  It gets
  142.    flushed when a newline is printed.   next_slot   points to the next
  143.    available location in the output buffer, i.e. where the next call to
  144.    nice_printf will have its output stored */
  145.  
  146. static char *output_buf;
  147. static char *next_slot;
  148. static char *string_start;
  149.  
  150. static char *word_start = NULL;
  151. static int cursor_pos = 0;
  152. static int In_string = 0;
  153.  
  154.  void
  155. np_init(Void)
  156. {
  157.     next_slot = output_buf = Alloc(MAX_OUTPUT_SIZE);
  158.     memset(output_buf, 0, MAX_OUTPUT_SIZE);
  159.     }
  160.  
  161.  static char *
  162. #ifdef KR_headers
  163. adjust_pointer_in_string(pointer)
  164.     register char *pointer;
  165. #else
  166. adjust_pointer_in_string(register char *pointer)
  167. #endif
  168. {
  169.     register char *s, *s1, *se, *s0;
  170.  
  171.     /* arrange not to break \002 */
  172.     s1 = string_start ? string_start : output_buf;
  173.     for(s = s1; s < pointer; s++) {
  174.         s0 = s1;
  175.         s1 = s;
  176.         if (*s == '\\') {
  177.             se = s++ + 4;
  178.             if (se > pointer)
  179.                 break;
  180.             if (*s < '0' || *s > '7')
  181.                 continue;
  182.             while(++s < se)
  183.                 if (*s < '0' || *s > '7')
  184.                     break;
  185.             --s;
  186.             }
  187.         }
  188.     return s0 - 1;
  189.     }
  190.  
  191. /* ANSI says strcpy's behavior is undefined for overlapping args,
  192.  * so we roll our own fwd_strcpy: */
  193.  
  194.  static void
  195. #ifdef KR_headers
  196. fwd_strcpy(t, s)
  197.     register char *t;
  198.     register char *s;
  199. #else
  200. fwd_strcpy(register char *t, register char *s)
  201. #endif
  202. { while(*t++ = *s++); }
  203.  
  204. /* isident -- true iff character could belong to a unit.  C allows
  205.    letters, numbers and underscores in identifiers.  This also doubles as
  206.    a check for numeric constants, since we include the decimal point and
  207.    minus sign.  The minus has to be here, since the constant "10e-2"
  208.    cannot be broken up.  The '.' also prevents structure references from
  209.    being broken, which is a quite acceptable side effect */
  210.  
  211. #define isident(x) (Tr[x] & 1)
  212. #define isntident(x) (!Tr[x])
  213.  
  214.   static void
  215. #ifdef KR_headers
  216.  ind_printf (use_indent, fp, a, b, c, d, e, f, g)
  217.   int use_indent;
  218.   FILE *fp;
  219.   char *a;
  220.   long b, c, d, e, f, g;
  221. #else
  222.  ind_printf (int use_indent, FILE *fp, char *a, va_list ap)
  223. #endif
  224. {
  225.     extern int max_line_len;
  226.     extern FILEP c_file;
  227.     extern char tr_tab[];    /* in output.c */
  228.     register char *Tr = tr_tab;
  229.     int ch, inc, ind;
  230.     static int extra_indent, last_indent, set_cursor = 1;
  231.  
  232.     cursor_pos += indent - last_indent;
  233.     last_indent = indent;
  234.     SPRINTF (next_slot, a, b, c, d, e, f, g);
  235.  
  236.     if (fp != c_file) {
  237.     fprintf (fp,"%s", next_slot);
  238.     return;
  239.     } /* if fp != c_file */
  240.  
  241.     do {
  242.     char *pointer;
  243.  
  244. /* The   for   loop will parse one output line */
  245.  
  246.     if (set_cursor) {
  247.         ind = indent <= MAX_INDENT
  248.             ? indent
  249.             : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  250.         cursor_pos = ind + extra_indent;
  251.         set_cursor = 0;
  252.         }
  253.     if (in_comment)
  254.             for (pointer = next_slot; *pointer && *pointer != '\n' &&
  255.                 cursor_pos <= max_line_len; pointer++)
  256.             cursor_pos++;
  257.     else
  258.           for (pointer = next_slot; *pointer && *pointer != '\n' &&
  259.         cursor_pos <= max_line_len; pointer++) {
  260.  
  261.         /* Update state variables here */
  262.  
  263.         if (In_string) {
  264.         switch(*pointer) {
  265.             case '\\':
  266.                 if (++cursor_pos > max_line_len) {
  267.                     cursor_pos -= 2;
  268.                     --pointer;
  269.                     goto overflow;
  270.                     }
  271.                 ++pointer;
  272.                 break;
  273.             case '"':
  274.                 In_string = 0;
  275.                 word_start = 0;
  276.             }
  277.         }
  278.         else switch (*pointer) {
  279.             case '"':
  280.             if (cursor_pos + 5 > max_line_len) {
  281.                 word_start = 0;
  282.                 --pointer;
  283.                 goto overflow;
  284.                 }
  285.             In_string = 1;
  286.             string_start = word_start = pointer;
  287.                 break;
  288.             case '\'':
  289.             if (pointer[1] == '\\')
  290.                 if ((ch = pointer[2]) >= '0' && ch <= '7')
  291.                     for(inc = 3; pointer[inc] != '\''
  292.                         && ++inc < 5;);
  293.                 else
  294.                     inc = 3;
  295.             else
  296.                 inc = 2;
  297.             /*debug*/ if (pointer[inc] != '\'')
  298.             /*debug*/  fatalstr("Bad character constant %.10s",
  299.                     pointer);
  300.             if ((cursor_pos += inc) > max_line_len) {
  301.                 cursor_pos -= inc;
  302.                 word_start = 0;
  303.                 --pointer;
  304.                 goto overflow;
  305.                 }
  306.             word_start = pointer;
  307.             pointer += inc;
  308.             break;
  309.         case '\t':
  310.             cursor_pos = 8 * ((cursor_pos + 8) / 8) - 1;
  311.             break;
  312.         default: {
  313.  
  314. /* HACK  Assumes that all characters in an atomic C token will be written
  315.    at the same time.  Must check for tokens first, since '-' is considered
  316.    part of an identifier; checking isident first would mean breaking up "->" */
  317.  
  318.             if (word_start) {
  319.             if (isntident(*(unsigned char *)pointer))
  320.                 word_start = NULL;
  321.             }
  322.             else if (isident(*(unsigned char *)pointer))
  323.             word_start = pointer;
  324.             break;
  325.         } /* default */
  326.         } /* switch */
  327.         cursor_pos++;
  328.     } /* for pointer = next_slot */
  329.  overflow:
  330.     if (*pointer == '\0') {
  331.  
  332. /* The output line is not complete, so break out and don't output
  333.    anything.  The current line fragment will be stored in the buffer */
  334.  
  335.         next_slot = pointer;
  336.         break;
  337.     } else {
  338.         char last_char;
  339.         int in_string0 = In_string;
  340.  
  341. /* If the line was too long, move   pointer   back to the character before
  342.    the current word.  This allows line breaking on word boundaries.  Make
  343.    sure that 80 character comment lines get broken up somehow.  We assume
  344.    that any non-string 80 character identifier must be in a comment.
  345. */
  346.  
  347.         if (*pointer == '\n')
  348.         in_define = 0;
  349.         else if (word_start && word_start > output_buf)
  350.         if (In_string)
  351.             if (string_start && pointer - string_start < 5)
  352.                 pointer = string_start - 1;
  353.             else {
  354.                 pointer = adjust_pointer_in_string(pointer);
  355.                 string_start = 0;
  356.                 }
  357.         else if (word_start == string_start
  358.                 && pointer - string_start >= 5) {
  359.             pointer = adjust_pointer_in_string(next_slot);
  360.             In_string = 1;
  361.             string_start = 0;
  362.             }
  363.         else
  364.             pointer = word_start - 1;
  365.         else if (cursor_pos > max_line_len) {
  366. #ifndef ANSI_Libraries
  367.         extern char *strchr();
  368. #endif
  369.         if (In_string) {
  370.             pointer = adjust_pointer_in_string(pointer);
  371.             if (string_start && pointer > string_start)
  372.                 string_start = 0;
  373.             }
  374.         else if (strchr("&*+-/<=>|", *pointer)
  375.             && strchr("!%&*+-/<=>^|", pointer[-1])) {
  376.             pointer -= 2;
  377.             if (strchr("<>", *pointer)) /* <<=, >>= */
  378.                 pointer--;
  379.             }
  380.         else {
  381.             if (word_start)
  382.                 while(isident(*(unsigned char *)pointer))
  383.                     pointer++;
  384.             pointer--;
  385.             }
  386.         }
  387.         last_char = *pointer;
  388.         write_indent(fp, use_indent, extra_indent, output_buf, pointer);
  389.         next_slot = output_buf;
  390.         if (In_string && !string_start && Ansi == 1 && last_char != '\n')
  391.         *next_slot++ = '"';
  392.         fwd_strcpy(next_slot, pointer + 1);
  393.  
  394. /* insert a line break */
  395.  
  396.         if (last_char == '\n') {
  397.         if (In_string)
  398.             last_was_newline = 0;
  399.         else {
  400.             last_was_newline = 1;
  401.             extra_indent = 0;
  402.             }
  403.         }
  404.         else {
  405.         extra_indent = TOO_LONG_INDENT;
  406.         if (In_string && !string_start) {
  407.             if (Ansi == 1) {
  408.                 fprintf(fp, "\"\n");
  409.                 use_indent = 1;
  410.                 last_was_newline = 1;
  411.                 }
  412.             else {
  413.                 fprintf(fp, "\\\n");
  414.                 last_was_newline = 0;
  415.                 }
  416.             In_string = in_string0;
  417.             }
  418.         else {
  419.             if (in_define)
  420.                 putc('\\', fp);
  421.             putc ('\n', fp);
  422.             last_was_newline = 1;
  423.             }
  424.         } /* if *pointer != '\n' */
  425.  
  426.         if (In_string && Ansi != 1 && !string_start)
  427.         cursor_pos = 0;
  428.         else
  429.         set_cursor = 1;
  430.  
  431.         string_start = word_start = NULL;
  432.  
  433.     } /* else */
  434.  
  435.     } while (*next_slot);
  436.  
  437. } /* ind_printf */
  438.