File: pl-bite.c

package info (click to toggle)
swi-prolog 3.1.0-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,772 kB
  • ctags: 12,869
  • sloc: ansic: 43,657; perl: 12,577; lisp: 4,359; sh: 1,534; makefile: 798; awk: 14
file content (98 lines) | stat: -rw-r--r-- 2,022 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*  $Id: pl-bite.c,v 1.10 1997/10/13 10:08:32 jan Exp $

    Copyright (c) 1990 Jan Wielemaker. All rights reserved.
    See ../LICENCE to find out about your rights.
    jan@swi.psy.uva.nl

    Purpose: Select part of a file
*/

#include "pl-incl.h"
#include <stdio.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is a very simple stand alone program that picks  a  region  from  a
file on character index basis and sends this to the standard output.  It
is  part  of the help system to display long sections of the manual.  It
should be installed as `pl-bite' in a directory that is in the  path  of
the user (normally /usr/local/bin, next to Prolog itself).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

static void usage(void);

char *program;
int  escape = FALSE;

int
main(int argc, char **argv)
{ long from, to;
  char *s, *range, *file;
  FILE *fd;
  

  program = argv[0];
  argc--, argv++;

  if ( argc >= 1 && streq(argv[0], "-e") )
  { escape = TRUE;
    argc--, argv++;
  }
  if ( argc != 2 || (s = strchr(argv[0], ':')) == NULL )
  { usage();
    exit(1);
  }
  range = argv[0];
  file = argv[1];

  *s = '\0';
  if ( (from = atol(range)) == 0 || (to = atol(&s[1])) == 0 )
  { usage();
    exit(1);
  }

  if ( (fd = fopen(file, "rb")) == NULL || fseek(fd, from, 0) < 0 )
  { perror(argv[2]);
    exit(1);
  }

  if ( !escape )
  { int c2 = getc(fd);

    while(from < to)
    { int c = c2;
      
      c2 = getc(fd);
      if ( c2 == '\b' )
      {	c2 = getc(fd);
	from += 2;
	continue;
      }
      putchar(c);
      from++;
    }
  } else
  { while(from++ < to)
    { Char c;
  
      if ( (c = getc(fd)) == EOF )
      { fprintf(stderr, "%s: %s: premature EOF\n", program, file);
	exit(1);
      }
      putchar(c);
    }
  }
  fflush(stdout);
  fclose(fd);

  exit(0);
  return(0);
}

static void
usage(void)
{ fprintf(stderr, "usage: %s [-e] from:to file\n", program);
  exit(1);
}