home *** CD-ROM | disk | FTP | other *** search
-
- #include "sless_defs.h"
- #include <libraries/sregexpbase.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <clib/dos_protos.h>
-
- Prototype void usage(void);
- Prototype void refreshscreen(void);
- Prototype void waitcommand(void);
- Prototype void leave(int, char *);
- Prototype int putline(void);
-
- const char memory[] = "sless: Unable to allocate memory.\n";
- const char end[] = " \x9b7m-- END OF FILE --\x9b0m";
- const char top[] = " \x9b7m-- TOP OF FILE --\x9b0m";
- const char nopat[] = " \x9b7m-- Pattern not found --\x9b0m";
- const char nolast[] = " \x9b7m-- No previous pattern --\x9b0m";
- const char wait[] = " \x9b7m-- Searching --\x9b0m";
- const char abort[] = " \x9b7m-- Search aborted --\x9b0m";
-
- char seek = 0;
- long startpos = 0,nextpos = 0;
- FILE *file = NULL,*ofile = NULL;
- BPTR con = 0;
- int sx,sy;
- struct SregExp *lastspat = NULL;
-
- int main(ac,av)
- int ac;
- char **av;
- {
- int line = 0,i;
- char *p,*oldwin = NULL;
-
- show_line;
- while (ac > 0) {
- ac--;
- av++;
- if (**av != '-')
- break;
- switch (*(*av+1)) {
- case ('l') :
- if (line != 0)
- usage();
- p = *av+2;
- while (isdigit(*p))
- line = line*10 + *p++ - '0';
- break;
- case ('c') :
- if (line != 0)
- usage();
- p = *av+2;
- while (isdigit(*p))
- line = line*10 - *p++ + '0';
- break;
- default :
- usage();
- }
- }
-
- if (ac < 1)
- usage();
- if (!(file = sopen(*av)))
- leave(15,"Unable to open input stream.\n");
-
- /* try and find the length of a file. If it fails or is zero, then
- we assume we cannot seek into the file. This catches trying to
- seek into pipes. */
- sseek(file,0,SEEK_END);
- if (stell(file) <= 0)
- seek = 0;
- else {
- seek = 1;
- sseek(file,0,SEEK_SET);
- }
-
- con = makeoutput(*av);
-
- if (line < 0) {
- startpos = -line;
- if (seek)
- sseek(file,startpos,SEEK_SET);
- else
- for (i = 0; i > line; i--)
- getc(file);
- } else {
- for (i = 0; i < line && (p = sgetline(file)); i++) {
- if (p[strlen(p)-1] == 12)
- i--;
- show_line;
- free(p);
- show_line;
- if (seek)
- startpos = stell(file);
- }
- }
-
- findsize(&sx,&sy,con);
- refreshscreen();
- waitcommand();
-
- closeoutput(con);
- con = 0;
- sclose(file);
- file = NULL;
- return 0;
- }
-
- void
- usage()
- {
- show_line;
- sputs("sless V"\
- VERSION\
- ", "\
- __DATE__\
- ", \xA9Copyright 1991 by Jon Spencer.\n");
- sputs("Usage: sless [-llineno | -ccharpos] file\n");
- sputs("Where lineno is the optional starting line number,\n");
- sputs("or charpos is the optional starting character positon.\n");
- exit(2);
- }
-
- void
- leave(x,p)
- int x;
- char *p;
- {
- show_line;
- sputs(p);
- closeoutput(con);
- if (con)
- closeoutput(con);
- if (file)
- sclose(file);
- if (ofile)
- sclose(ofile);
- exit(x);
- }
-
- void
- waitcommand()
- {
- short c;
-
- show_line;
- do {
- switch (c = getconchar(con)) {
- case (' ') :
- sgetcur(file);
- if (seof(file) && nextpos > 0)
- nextpos = -1;
- if (nextpos >= 0) {
- startpos = nextpos;
- refreshscreen();
- } else {
- bottomline();
- if (nextpos-- == -2)
- return;
- putconstr(end,con);
- }
- break;
- case ('<') :
- case (makeconid('T',0)) :
- if (seek) {
- if (startpos == 0) {
- bottomline();
- putconstr(top,con);
- } else {
- startpos = 0;
- refreshscreen();
- }
- }
- break;
- case (13) :
- case (makeconid('B',0)) :
- sgetcur(file);
- if (seof(file) && nextpos > 0)
- nextpos = -1;
- if (nextpos >= 0) {
- downone();
- } else {
- bottomline();
- if (nextpos-- == -2)
- return;
- putconstr(end,con);
- }
- break;
- case ('q') :
- bottomline();
- return;
- case (8) :
- case ('b') :
- if (seek) {
- if (startpos == 0) {
- bottomline();
- putconstr(top,con);
- } else
- backup(sy-2);
- }
- break;
- case ('d') :
- sgetcur(file);
- if (seof(file) && nextpos > 0)
- nextpos = -1;
- if (nextpos >= 0) {
- int i = 1;
-
- while (i++ < sy/2 && nextpos > 0)
- startpos = skiponeline(startpos);
- refreshscreen();
- } else {
- bottomline();
- if (nextpos-- == -2)
- return;
- putconstr(end,con);
- }
- break;
- case ('>') :
- case (makeconid('S',0)) :
- if (seek) {
- sseek(file,-1,SEEK_END);
- startpos = stell(file);
- backup(sy-1);
- }
- break;
- case ('u') :
- if (seek) {
- if (startpos != 0) {
- backup(sy/2-1);
- } else {
- bottomline();
- putconstr(top,con);
- }
- }
- break;
- case (makeconid('A',0)) :
- if (seek) {
- if (startpos != 0) {
- uponeline();
- } else {
- bottomline();
- putconstr(top,con);
- }
- }
- break;
- case ('/') :
- if (seek) {
- bottomline();
- putconstr("/",con);
- if (!searchfor())
- break;
- nextpat();
- }
- break;
- case ('n') :
- if (seek)
- nextpat();
- break;
- case ('p') :
- if (seek)
- prevpat();
- break;
- case (';') :
- if (seek) {
- bottomline();
- putconstr(";",con);
- if (!searchfor())
- break;
- prevpat();
- }
- break;
- case (makeconid('?','~')) :
- case ('h') :
- case ('?') :
- if (!ofile)
- behelpfull();
- break;
- }
- } while (TRUE);
- }
-
- void
- refreshscreen()
- {
- int y,c;
- long t;
-
- show_line;
- putconstr("\x9b0 p\x0c",con);
-
- y = sy;
- if (seek)
- sseek(file,startpos,SEEK_SET);
- do {
- if (seek && y > 1)
- nextpos = stell(file);
- } while (--y > 0 && !(c = putline()));
-
- if (seof(file))
- nextpos = -1;
- if (c == 2) {
- nextpos = stell(file);
- }
-
- while (--y > 0)
- putconchar(10,con);
-
- putconstr(": \x9b p",con);
- }
-
- int
- putline()
- {
- char *p,*q;
-
- show_line;
-
- if (!(p = sgetline(file))) {
- putconchar(10,con);
- return TRUE;
- }
- q = skipdispline(p);
- if (*(q-1) == 12) {
- *(q-1) = 0;
- putconstr(p,con);
- putconstr("^L\n",con);
- free(p);
- return 2;
- } else if (*q) {
- sseek(file,q - p - strlen(p),SEEK_CUR);
- *q++ = '\n';
- *q = 0;
- }
- putconstr(p,con);
- free(p);
- return seof(file);
- }
-
- /*
- #include <stdarg.h>
-
- Prototype int printf(const char *, ...);
- extern int vsprintf(char *, const char *, va_list);
-
- int printf(f, ...)
- const char *f;
- {
- char buff[100];
- va_list va;
-
- va_start(va,f);
- vsprintf(buff,f,va);
- va_end(va);
- Write(Output(),buff,strlen(buff));
- }
- */
-