Add PGINDENT and support program.
authorBruce Momjian
Fri, 29 Aug 1997 21:13:00 +0000 (21:13 +0000)
committerBruce Momjian
Fri, 29 Aug 1997 21:13:00 +0000 (21:13 +0000)
src/tools/PGINDENT [new file with mode: 0644]
src/tools/entab/Makefile [new file with mode: 0644]
src/tools/entab/entab.c [new file with mode: 0644]
src/tools/entab/entab.man [new file with mode: 0644]
src/tools/entab/halt.c [new file with mode: 0644]

diff --git a/src/tools/PGINDENT b/src/tools/PGINDENT
new file mode 100644 (file)
index 0000000..2b0b727
--- /dev/null
@@ -0,0 +1,24 @@
+#!/bin/sh
+trap "rm -f /tmp/$$" 0 1 2 3 15
+entab /dev/null
+if [ "$?" -ne 0 ]
+then   echo "Go to the src/tools/entab directory and do a 'make' and 'make install'." >&2
+   echo "This will put the 'entab' command in your path." >&2
+   echo "Then run $0 again."
+   exit 1
+fi
+indent -st /dev/null
+if [ "$?" -ne 0 ]
+then   echo "You do not appear to have 'indent' installed on your system." >&2
+   exit 1
+fi
+for FILE
+do
+   cat $FILE |
+   sed 's;/\*  *---;/*---;g' |
+   indent -bad -bap -bbb -bc -bl -d0 -ncdb -nce -cli1 -di16 -nfc1 \
+       -lp -nip -nbc -psl -di1 -i4 -st |
+   detab -t8 |
+   entab -qc -t4 |
+   sed 's;/\*---;/* ---;g' >/tmp/$$ && cat /tmp/$$ >$FILE
+done
diff --git a/src/tools/entab/Makefile b/src/tools/entab/Makefile
new file mode 100644 (file)
index 0000000..d56accb
--- /dev/null
@@ -0,0 +1,29 @@
+#
+# Makefile
+#
+#
+TARGET = entab
+BINDIR = /usr/local/bin
+XFLAGS = 
+CFLAGS = -O
+LIBS = 
+
+$(TARGET) : entab.o halt.o
+   $(CC) -o $(TARGET) $(XFLAGS) $(CFLAGS) entab.o halt.o $(LIBS)
+
+entab.o    : entab.c 
+   $(CC) -c $(XFLAGS) $(CFLAGS) entab.c
+
+halt.o : halt.c 
+   $(CC) -c $(XFLAGS) $(CFLAGS) halt.c
+
+clean:
+   rm -f *.o $(TARGET) log core
+
+install:
+   make clean
+   make CFLAGS=-O
+   install -s -o bin -g bin $(TARGET) $(BINDIR)
+   rm -f $(BINDIR)/detab
+   ln /usr/local/bin/$(TARGET) $(BINDIR)/detab
+
diff --git a/src/tools/entab/entab.c b/src/tools/entab/entab.c
new file mode 100644 (file)
index 0000000..e04d853
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+** entab.c     - add tabs to a text file
+** by Bruce Momjian ([email protected])
+**
+**  version 1.0
+**
+** tabsize = 4
+**
+*/
+
+#include 
+#include 
+#include 
+
+#define NUL        '\0'
+
+#ifndef TRUE
+#define    TRUE    1
+#endif
+#ifndef FALSE
+#define FALSE  0
+#endif
+
+void halt();
+extern char *optarg;
+extern int optind;
+
+int main(argc, argv)
+int argc;
+char **argv;
+{
+   int tab_size = 8,
+       min_spaces = 2,
+       protect_quotes = FALSE,
+       del_tabs = FALSE,
+       clip_lines = FALSE,
+       prv_spaces,
+       col_in_tab,
+       escaped,
+       nxt_spaces;
+   char in_line[BUFSIZ],
+        out_line[BUFSIZ],
+        *src,
+        *dst,
+        quote_char,
+        ch,
+        *cp;
+   FILE    *in_file;
+
+   if ((cp = strrchr(argv[0],'/')) != NULL)
+       ++cp;
+   else
+       cp = argv[0];
+   if (strcmp(cp,"detab") == 0)
+       del_tabs = 1;
+                                                                
+   while ((ch = getopt(argc, argv, "cdhqs:t:")) != -1)
+       switch (ch)
+       {
+           case 'c' : clip_lines = TRUE; break;
+           case 'd' : del_tabs = TRUE; break;
+           case 'q' : protect_quotes = TRUE; break;
+           case 's' : min_spaces = atoi(optarg); break;
+           case 't' : tab_size = atoi(optarg); break;
+           case 'h' :
+           case '?' :
+               halt("USAGE: %s [ -cdqst ] [file ...]\n\
+   -c (clip trailing whitespace)\n\
+   -d (delete tabs)\n\
+   -q (protect quotes)\n\
+   -s minimum_spaces\n\
+   -t tab_width\n",
+                   cp);
+       }
+
+   argv += optind;
+   argc -= optind;
+   
+   do {
+       if (argc < 1)
+           in_file = stdin;
+       else
+       {
+           if ( (in_file=fopen(*argv,"r")) == NULL)
+               halt("PERROR:  Can not open file %s\n",argv[0]);
+           argv++;
+       }
+
+       escaped = FALSE;
+
+       while (fgets(in_line, BUFSIZ, in_file) != NULL)
+       {
+           col_in_tab = 0;
+           prv_spaces = 0;
+           src = in_line;      /* points to current processed char */
+           dst = out_line;     /* points to next unallocated char */
+           if (escaped == FALSE)
+               quote_char = ' ';
+           escaped = FALSE;
+           while (*src != NUL)
+           {
+               col_in_tab++;
+               if (*src == ' ' || *src == '\t')
+               {
+                   if (*src == '\t')
+                   {
+                       prv_spaces = prv_spaces + tab_size - col_in_tab + 1;
+                       col_in_tab = tab_size;
+                   }
+                   else
+                       prv_spaces++;
+
+                   if (col_in_tab == tab_size)
+                   {
+                       /* Is the next character going to be a tab?
+                          Needed to do tab replacement in current spot if
+                          next char is going to be a tab, ignoring
+                          min_spaces */
+                       nxt_spaces = 0;
+                       while (1)
+                       {
+                           if ( *(src+nxt_spaces+1) == NUL ||
+                               (*(src+nxt_spaces+1) != ' ' &&
+                                *(src+nxt_spaces+1) != '\t'))
+                               break;
+                           if (*(src+nxt_spaces+1) == ' ')
+                               ++nxt_spaces;
+                           if (*(src+nxt_spaces+1) == '\t' || 
+                               nxt_spaces == tab_size)
+                           {
+                               nxt_spaces = tab_size;
+                               break;
+                           }
+                       }
+                       if ((prv_spaces >= min_spaces || nxt_spaces == tab_size) &&
+                            quote_char == ' ' &&
+                            del_tabs == FALSE )
+                       {
+                           *(dst++) = '\t';
+                           prv_spaces = 0;
+                       }
+                       else
+                       {
+                           for (; prv_spaces > 0; prv_spaces--)
+                               *(dst++) = ' ';
+                       }
+                   }
+               }
+               else
+               {
+                   for (; prv_spaces > 0; prv_spaces--)
+                       *(dst++) = ' ';
+                   if (*src == '\b')
+                       col_in_tab -= 2;
+                   if (escaped == FALSE && protect_quotes == TRUE)
+                   {
+                       if (*src == '\\')
+                           escaped = TRUE;
+                       if (*src == '"' || *src == '\'')
+                           if (quote_char == ' ')
+                                   quote_char = *src;
+                           else if (*src == quote_char)
+                                   quote_char = ' ';
+                   }
+                   else
+                       if (*src != '\r' && *src != '\n')
+                           escaped = FALSE;
+                           
+                   if (( *src == '\r' || *src == '\n') &&
+                       clip_lines == TRUE && escaped == FALSE)
+                   {
+                       while (dst > out_line &&
+                             (*(dst-1) == ' ' || *(dst-1) == '\t'))
+                                   dst--;
+                       prv_spaces = 0;
+                   }
+                   *(dst++) = *src;
+               }
+               col_in_tab %= tab_size;
+               ++src;
+           }
+           /* for cases where the last line of file has no newline */
+           if (clip_lines == TRUE && escaped == FALSE)
+           {
+               while (dst > out_line &&
+                     (*(dst-1) == ' ' || *(dst-1) == '\t'))
+                           dst--;
+               prv_spaces = 0;
+           }
+           for (; prv_spaces > 0; prv_spaces--)
+               *(dst++) = ' ';
+           *dst = NUL;
+           if (fputs(out_line,stdout) == EOF)
+               halt("PERROR:  Error writing output.\n");
+       }
+   } while (--argc > 0);
+   return 0;
+}
diff --git a/src/tools/entab/entab.man b/src/tools/entab/entab.man
new file mode 100644 (file)
index 0000000..c6c2e7b
--- /dev/null
@@ -0,0 +1,51 @@
+.TH ENTAB 1 local
+.SH NAME
+entab - tab processor
+.SH SYNOPSIS
+.nf
+entab [-cdq] [-s min_spaces] [-t tab_width] [file ... ]
+detab [-cq]  [-s min_spaces] [-t tab_width] [file ... ]
+.fi
+.SH DESCRIPTION
+Entab is a program designed to selectively add or remove tabs
+from a file based on user-supplied criteria.
+In default mode, entab prints the specified files to standard output
+with the optimal mix of tabs and spaces.
+Tabs default to every 8 characters, and tabs are used only when they
+can replace more than one space, unlike 'col' which uses tabs wherever
+possible.
+.LP
+The options are:
+.in +0.5i
+.nf
+-c     Clip trailing tabs and spaces from each line.
+-d Delete all tabs from output
+-q Protect single and double-quoted strings from tab replacement.
+   (This option is useful when operating on source code.
+    Line continuation with back-slashes is also understood.)
+-s     Minimum spaces needed to replace with a tab (default = 2).
+-t Number of spaces in a tab stop (default = 8).
+.fi
+.in -0.5i
+Detab is equivalent to entab -d.
+.SH NOTES
+Entab has improved tab handling for certain situations.
+It only replaces tabs if there is a user-defined number of spaces
+to be saved.
+Other tab replacement programs put tabs wherever
+possible, so if two words are separated by one space, and that
+space is on a tab stop, a tab is inserted.
+Then, when words are added to the left, the words are shifted over,
+leaving a large gap.
+The quote-protection option allows tab replacement without
+quoted strings being changed.
+Useful when strings in source code will not have the same tab stops
+when executed in the program. 
+.LP
+To change a text file created on a system with one size of tab
+stop to display properly on a device with different tab setting,
+use detab (or entab -d) to remove tabs from the file with the
+tab size set to the original tab size, then use entab to re-tab
+the file with the new tab size.
+.SH AUTHOR
+Bruce Momjian, [email protected]
diff --git a/src/tools/entab/halt.c b/src/tools/entab/halt.c
new file mode 100644 (file)
index 0000000..7df7a45
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+**
+** halt.c
+**
+** This is used to print out error messages and exit
+*/
+
+#include 
+#include 
+#include 
+#include 
+
+
+/*-------------------------------------------------------------------------
+**
+** halt - print error message, and call clean up routine or exit
+**
+**------------------------------------------------------------------------*/
+
+/*VARARGS*/
+void halt(va_alist)
+va_dcl
+{
+   va_list arg_ptr;
+   char    *format, *pstr;
+   void (*sig_func)();
+
+   va_start(arg_ptr);
+   format = va_arg(arg_ptr,char *);
+   if (strncmp(format,"PERROR", 6) != 0)
+       vfprintf(stderr,format,arg_ptr);
+   else
+   {
+       for (pstr=format+6; *pstr == ' ' || *pstr == ':'; pstr++)
+           ;
+       vfprintf(stderr,pstr,arg_ptr);
+       perror("");
+   }           
+   va_end(arg_ptr);
+   fflush(stderr);
+
+       /* call one clean up function if defined */
+   if ( (sig_func = signal(SIGTERM, SIG_DFL)) != SIG_DFL &&
+         sig_func != SIG_IGN)
+       (*sig_func)(0);
+   else if ( (sig_func = signal(SIGHUP, SIG_DFL)) != SIG_DFL &&
+               sig_func != SIG_IGN)
+       (*sig_func)(0);
+   else if ( (sig_func = signal(SIGINT, SIG_DFL)) != SIG_DFL &&
+               sig_func != SIG_IGN)
+       (*sig_func)(0);
+   else if ( (sig_func = signal(SIGQUIT, SIG_DFL)) != SIG_DFL &&
+               sig_func != SIG_IGN)
+       (*sig_func)(0);
+   exit(1);
+}