#include /* Pos - a text positioning tool */ /* Written by Jonathan Bowen, 15 July 1985 */ /* Version 1.1, October 1985 (error returns improved) */ /* Version 1.2, January 1990 (blank lines give zero-length lines) */ #define USAGE "Usage: pos [ -b ] [ column ] [ file ... ]\n" #define FALSE 0 #define TRUE 1 #define NUL '\0' #define HT '\t' #define LF '\n' #define SP ' ' #define TABLENGTH 8 /* Must be a power of 2 */ #define MAXLENGTH 256 /* Must be a multiple of TABLENGTH */ #define DEFAULTPOS 40 /* Column positioning */ #define NOPOS (-1) /* Invalid column position */ extern errno; /* Global error number */ column = DEFAULTPOS; /* Column position */ blanks = FALSE; /* Leading white spaces include tabs */ first = FALSE; /* First line used for adjustment */ left = FALSE; /* Left margin adjustment */ right = FALSE; /* Right margin adjustment */ main(argc, argv) int argc; char *argv[]; { FILE *fp, *fopen(); errno = 0; if (argc > 1) { if ((*++argv)[0] == '-') { switch (*(argv[0]+1)) { case 'b': blanks = TRUE; argc--; *argv++; break; case 'U': fprintf(stderr,USAGE); exit(0); } } } if (argc > 1) { if (*(argv[0]) == '-') left = TRUE; else if (*(argv[0]) == '+') right = TRUE; if ((left || right || (*(argv[0]) == '0')) && (*(argv[0]+1) == '\0')) /* `-' or `+' or `0' */ first = TRUE; /* position calculated from 1st line */ if (! first) { /* check for valid column */ column = abs(atoi(argv[0])); if (((left || right) && (column <= 0)) || (column>MAXLENGTH)) { fprintf(stderr, "pos: Bad column position (1-%d): %d\n", MAXLENGTH, column); exit(1); } } if (column == 0) { /* invalid column - assume file name */ column = DEFAULTPOS; *argv--; } else argc--; } column--; /* start from column 0 rather than column 1 */ if (argc == 1) /* no arguments left; read standard input */ position(stdin); else while (--argc > 0) if ((fp = fopen(*++argv,"r")) == NULL) { perror(*argv); /* No such file or directory */ continue; } else { position(fp); if (fclose(fp) == EOF) { perror(*argv); /* Can't close file */ continue; } } exit(errno); } position(fp) /* position file fp on standard output */ FILE *fp; { char c; char buf[MAXLENGTH+1]; int len, old, pos, pad, tabs, spaces, i; if (first) column = NOPOS; while ((c=getc(fp)) != EOF) { if (c == '.') { /* lines starting with `.' not mangled */ while ((c != LF) && (c != EOF)) { putchar(c); c = getc(fp); } putchar(LF); } else { len=0; pos=0; while ((c == SP) || (c == HT)) { /* leading white spaces */ if (c == HT) pos |= TABLENGTH-1; pos++; c = getc(fp); #ifdef DEBUG printf("pos = %d\n",pos); #endif } while ((c != LF) && (c != EOF) && (len <= MAXLENGTH)) { /* valid line */ old = pos; if (c == HT) pos |= TABLENGTH-1; pos++; if (c == HT) { spaces = pos-old; for (i=0; i 0) { /* trailing white spaces */ if (buf[len-1] == SP) buf[--len] = NUL; else break; #ifdef DEBUG printf("len = %d\n",len); #endif } if (column == NOPOS) { if (left) column = pos - len; else if (right) column = pos; else /* if (center) */ column = pos - len/2; } if (left) pad = column; else if (right) pad = column - len; else /* if (center) */ pad = column - len/2; if ((pad < 0) || (len == 0)) pad = 0; if (blanks) { tabs = 0; spaces = pad; } else { tabs = pad/TABLENGTH; spaces = pad%TABLENGTH; } for (i=0; i