|
|
1.1 root 1: #include <stdio.h>
2: #include <string.h>
3:
4: #include "dirwrap.h"
5:
6: #include "ciolib.h"
7: #include "keys.h"
8: #include "cterm.h"
9:
10: #define SCROLL_LINES 1000
11: #define BUF_SIZE 1024
12:
13:
14: void viewscroll(void)
15: {
16: int top;
17: int key;
18: int i;
19: char *scrollback;
20: struct text_info txtinfo;
21:
22: gettextinfo(&txtinfo);
23: /* ToDo: Watch this... may be too large for alloca() */
24: scrollback=(char *)malloc((txtinfo.screenwidth*2*SCROLL_LINES)+(txtinfo.screenheight*txtinfo.screenwidth*2));
25: memcpy(scrollback,cterm.scrollback,txtinfo.screenwidth*2*SCROLL_LINES);
26: gettext(1,1,txtinfo.screenwidth,txtinfo.screenheight,scrollback+(cterm.backpos)*cterm.width*2);
27: top=cterm.backpos;
28: for(i=0;!i;) {
29: if(top<1)
30: top=1;
31: if(top>cterm.backpos)
32: top=cterm.backpos;
33: puttext(1,1,txtinfo.screenwidth,txtinfo.screenheight,scrollback+(txtinfo.screenwidth*2*top));
34: key=getch();
35: switch(key) {
36: case 0xff:
37: case 0:
38: switch(key|getch()<<8) {
39: case CIO_KEY_UP:
40: top--;
41: break;
42: case CIO_KEY_DOWN:
43: top++;
44: break;
45: case CIO_KEY_PPAGE:
46: top-=txtinfo.screenheight;
47: break;
48: case CIO_KEY_NPAGE:
49: top+=txtinfo.screenheight;
50: break;
51: }
52: break;
53: case 'j':
54: case 'J':
55: top--;
56: break;
57: case 'k':
58: case 'K':
59: top++;
60: break;
61: case 'h':
62: case 'H':
63: top-=txtinfo.screenheight;
64: break;
65: case 'l':
66: case 'L':
67: top+=txtinfo.screenheight;
68: break;
69: case '\033':
70: i=1;
71: break;
72: }
73: }
74: free(scrollback);
75: return;
76: }
77:
78: void lfexpand(char *buf, int *len)
79: {
80: char newbuf[BUF_SIZE*2];
81: int newlen=0;
82: int i;
83:
84: for(i=0; i<*len; i++) {
85: if(buf[i]=='\n') {
86: newbuf[newlen++]='\r';
87: }
88: newbuf[newlen++]=buf[i];
89: }
90: *len=newlen;
91: memcpy(buf, newbuf, newlen);
92:
93: return;
94: }
95:
96: int main(int argc, char **argv)
97: {
98: struct text_info ti;
99: FILE *f;
100: char buf[BUF_SIZE*2]; /* Room for lfexpand */
101: int len;
102: int speed;
103: char *scrollbuf;
104: char *infile=NULL;
105: char title[MAX_PATH+1];
106: int expand=0;
107: int i;
108:
109: textmode(C80);
110: gettextinfo(&ti);
111: if((scrollbuf=malloc(SCROLL_LINES*ti.screenwidth*2))==NULL) {
112: cprintf("Cannot allocate memory\n\n\rPress any key to exit.");
113: getch();
114: return(-1);
115: }
116:
117: /* Parse command line */
118: for(i=1; i<argc; i++) {
119: if(argv[i][0]=='-') {
120: if(argv[i][1]=='l' && argv[i][2]==0)
121: expand=1;
122: else
123: goto usage;
124: }
125: else {
126: if(infile==NULL)
127: infile=argv[i];
128: else
129: goto usage;
130: }
131: }
132:
133: cterm_init(ti.screenheight, ti.screenwidth, 0, 0, SCROLL_LINES, scrollbuf);
134: if(infile) {
135: if((f=fopen(infile,"r"))==NULL) {
136: cprintf("Cannot read %s\n\n\rPress any key to exit.",argv[1]);
137: getch();
138: return(-1);
139: }
140: sprintf(title,"SyncView: %s",getfname(argv[1]));
141: }
142: else {
143: f=stdin;
144: strcpy(title,"SyncView: [stdin]");
145: }
146: settitle(title);
147: while((len=fread(buf, 1, BUF_SIZE, f))!=0) {
148: if(expand)
149: lfexpand(buf, &len);
150: cterm_write(buf, len, NULL, 0, &speed);
151: }
152: viewscroll();
153: return(0);
154:
155: usage:
156: cprintf("Usage: %s [-l] [filename]\r\n\r\n"
157: "Displays the ANSI file filename expanding \\n to \\r\\n if -l is specified.\r\n"
158: "If no filename is specified, reads input from stdin\r\n"
159: "\r\n"
160: "Press any key to exit.");
161: getch();
162: return(-1);
163: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.