1 /*
2 * view.c
3 *
4 * Copyright (C) 1997, 1998, 2000, 2003, 2004, 2006, 2007 Staf Wagemakers Belgium
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22 #include "view.h"
23 static VIEW_PAR *p;
view_par(VIEW_PAR * par)24 VIEW_PAR *view_par (VIEW_PAR *par)
25 {
26 if (par!=NULL) p=par;
27 return (p);
28 }
view_strlen(char * c)29 unsigned view_strlen(char *c)
30 {
31 unsigned u=0;
32 while (*c) {
33 if (*c!=8) ++u;
34 ++c;
35 }
36 return u;
37 }
view_gety()38 unsigned long view_gety()
39 {
40 if (p->mode) return(p->y+p->sy-p->lines+1);
41 else return(p->y-p->lines+1);
42 }
view_getx()43 unsigned long view_getx()
44 {
45 if (p->mode) return(p->x+p->sx+1);
46 else return(p->x+1);
47 }
view_getstr(unsigned long y)48 char *view_getstr(unsigned long y)
49 {
50 char *rc,*c,*s;
51 s=p->file[y];
52 c=rc=(char *) xmalloc(strlen(s)+1);
53 while (*s)
54 { if (*s!=8) { *c=*s;++c;}
55 else if(c>rc) --c;
56 ++s;
57 }
58 *c=0;
59 rc=(char *) xrealloc(rc,strlen(rc)+1);
60 return rc;
61 }
62 /* -------------------------------------------- */
63 /* Wis een het bestand uit het geheugen ... */
64 /* -------------------------------------------- */
view_free_file()65 void view_free_file()
66 {
67 unsigned long l;
68 for (l=0;l<=p->y_max;l++) free(p->file[l]);
69 xfree(p->file);
70 }
view_free()71 void view_free()
72 {
73 view_free_file();
74 free_string_array(*p->view_exec);
75 }
get_strlen(char * s)76 int get_strlen (char *s)
77 {
78 int xmax=0;
79 while (*s) {
80 if (*s!=8) ++xmax;
81 else --xmax;
82 s++;
83 }
84 return xmax;
85 }
86 /* ---------------------------------------------------- */
87 /* Inladen van te bekijken bestand ... */
88 /* */
89 /* aanroep : */
90 /* */
91 /* char filename[] = te lezen bestand */
92 /* char **file = ptr naar geh. pl. */
93 /* */
94 /* antwoord : */
95 /* 0 = ok */
96 /* 1 = nok */
97 /* */
98 /* ---------------------------------------------------- */
view_load()99 int view_load ()
100 {
101 FILE *fp=NULL;
102 char *cp,***ccc;
103 char *s;
104 int i;
105 size_t ii;
106 /* ----------------- vari voor het starten van gzip ---------------------- */
107 int is_gz=0;
108 char *gzip=NULL;
109 FILE *popen(),*gz_fp;
110 /* ----------------------------------------------------------------------- */
111 ccc=p->view_exec;
112 if(p->filename!=NULL) {
113 while(*ccc) {
114 if(strlen(p->filename)>strlen(ccc[0][0])) {
115 s=p->filename+strlen(p->filename)-strlen(ccc[0][0]);
116 }
117 else {
118 s=p->filename;
119 }
120 if(!strcmp(s,ccc[0][0])){
121 gzip=(char *)xmalloc(strlen(ccc[0][1])+strlen(p->filename)+2);
122 strcpy(gzip,ccc[0][1]);
123 strcat(gzip," ");
124 strcat(gzip,p->filename);
125 break;
126 }
127 ++ccc;
128 }
129 }
130
131 if(gzip) {
132 int sys_err;
133
134 if (p->cmd==1) freopen("/dev/null","w",stderr);
135 freopen("/dev/null","w",stdout);
136 sys_err=system(gzip);
137 if (sys_err==0) {
138 gz_fp=popen(gzip,"r");
139 if (gz_fp==NULL) pclose(fp);
140 else is_gz=1;
141 }
142
143 freopen("/dev/tty","w",stdout);
144 if (p->cmd==1) freopen("/dev/tty","w",stderr);
145 free(gzip);
146 }
147 if (p->cmd<2) {
148 if ((fp=fopen(p->filename,"r"))==NULL) {
149 if (p->cmd==1) {
150 fprintf(stderr,"%s %s %s",p->txt_f_open1,p->filename,p->txt_f_open2);
151 if (COLS) endwin();
152 exit(1);
153 }
154 else {
155 char *txt[]={p->txt_f_load,NULL};
156 open_okwin(6,40,p->m_ok,txt,p->win);
157 return(1);
158 };
159 };
160 };
161 if (p->load) view_free_file();
162 p->y=p->sy=0;p->sx=p->x=0;p->x_max=0;p->y_max=0;
163 if (p->cmd<2) {
164 if (fseek(fp,0,SEEK_END)) {
165 char *txt[]={p->txt_f_fseek,NULL};
166 open_okwin(6,40,p->m_ok,txt,p->win);
167 return(1);
168 };
169 };
170 if (p->cmd<2) {
171 p->size=ftell(fp);
172 fseek(fp,0,SEEK_SET); }
173 else p->size=0;
174 if (is_gz) {fclose(fp);fp=gz_fp;};
175 p->file=NULL;
176 ii=0;
177 do {
178 int z;
179 p->file=(char **) xrealloc(p->file,(p->y_max+2)*sizeof(char **));
180 *(p->file+p->y_max)=NULL;
181 if (p->cmd==2) p->x=getdelim(p->file+p->y_max,&ii,'\n',stdin);
182 else p->x=getdelim(p->file+p->y_max,&ii,'\n',fp);
183 if (p->x>0) {
184
185 if (p->cmd==2) p->size+=p->x;
186
187 if (p->x>1) {
188
189 if (*(p->file[p->y_max]+p->x-2)==13) {
190 *(p->file[p->y_max]+p->x-2)=10;
191 *(p->file[p->y_max]+p->x-1)=0;
192 };
193 }
194
195
196 for (i=0;i<strlen(p->file[p->y_max]);i++) {
197 if (*(p->file[p->y_max]+i)==TAB) {
198 s=(char *) xmalloc(strlen(p->file[p->y_max])+(((i/8)+1)*8-i)+1);
199 strcpy(s,p->file[p->y_max]);
200 for (z=0;z<((i/8)+1)*8-i;z++) strcpy((s+i+z)," ");
201 strcpy((s+i+z),(p->file[p->y_max]+i+1));
202 cp=*(p->file+p->y_max);
203 cp=(char *)xrealloc(cp,strlen(s)+1);
204 *(p->file+p->y_max)=cp;
205 strcpy(p->file[p->y_max],s);
206 free(s);
207 }
208 }
209 if ((get_strlen(p->file[p->y_max])>p->x_max)&&(p->x!=-1)) p->x_max=get_strlen(p->file[p->y_max]);
210 ++p->y_max;
211 }
212 } while (p->x!=-1);
213 if ((p->cmd<2)&&(!is_gz)) fclose(fp);
214 if (is_gz) pclose(fp);
215 p->load=1;
216 if (p->x_max>p->cols) p->x_max-=p->cols;
217 else p->x_max=0;
218 p->x=p->y=0;
219 return(0);
220 }
221 /* ----------------------------------------- */
222 /* Print een kar. op het scherm, filter voor */
223 /* '/0' en 0x0ad. */
224 /* ----------------------------------------- */
view_addch(char * c)225 void view_addch(char *c)
226 {
227 /* VIEW_PAR *p=view_par(NULL); */
228 if (*c==(char)0xad) waddch_fix(p->win,'-');
229 else if (*(c)) waddch_fix(p->win,*c);
230 }
231 /* ----------------------------------------- */
232 /* Print een regel uit **bestand af ... */
233 /* */
234 /* int yp = y-postie */
235 /* unsigned long r = regel */
236 /* ----------------------------------------- */
view_addline(int yp,unsigned long r)237 void view_addline (int yp,unsigned long r)
238 {
239 char *s;
240 unsigned t;
241 int lx=0;
242 if (r>=p->y_max) return;
243 if (strlen(p->file[r])>p->x) {
244 if (p->y<=p->y_max) {
245 s=p->file[r];
246 lx=strlen(s);
247 if (p->x) {
248 unsigned tt=0;
249 for (t=0;t<=p->x-1;t++) {
250 if (*(s+1)==8) {s+=3;tt+=3;}
251 else {++s;++tt;}
252 if (tt>=lx) return;
253 }
254 }
255 wmove(p->win,yp,0);
256 view_addstr(s);
257 }
258 }
259 }
view_addstr(char * s)260 void view_addstr(char *s)
261 {
262 int lx=0;
263 unsigned t;
264 for (t=0;t<=strlen(s);t++) {
265 if (((lx>p->cols-1)&&*(s+t)!=8)||(*(s+t)==0)) break;
266 if (*(s+t)==8) {
267 lx--;
268 if (*(s+t-1)==*(s+t+1)) {
269 waddch_fix(p->win,*(s+t));
270 wbkgdset(p->win,p->color[2]);
271 view_addch(s+(++t));
272 wbkgdset(p->win,p->color[1]);
273 }
274 else {
275 if (*(s+t-1)=='_') {
276 waddch_fix(p->win,*(s+t));
277 wbkgdset(p->win,p->color[3]);
278 view_addch(s+(++t));
279 wbkgdset(p->win,p->color[1]);
280 }
281 else waddch_fix(p->win,'.');
282 }
283 }
284 else view_addch(s+t);
285 lx++;
286 }
287 }
view_sety(unsigned long y)288 void view_sety(unsigned long y)
289 {
290 if (!p->mode) {
291 if (y > p->y_max) y=p->y_max;
292 p->y=y;
293 return;
294 }
295 if (y > p->y_max-1) y=p->y_max-1;
296 if (y > (p->y_max-p->lines-1)) {
297 p->y=p->y_max-p->lines;
298 p->sy=y-p->y;
299 }
300 else {
301 p->y=(y/p->lines)*p->lines;
302 p->sy=y%p->lines;
303 }
304 }
view_setx(long x)305 void view_setx(long x)
306 {
307 p->x=x/p->cols;
308 p->sx=x%p->cols;
309 p->x*=p->cols;
310 }
view_move(unsigned long y,long x)311 void view_move (unsigned long y, long x)
312 {
313 view_sety(y);
314 view_setx(x);
315 view_refresh();
316 }
view_set_cursor(void)317 void view_set_cursor(void)
318 {
319 wmove(p->win,p->sy,p->sx);
320 }
321 /* ----------------------------------------- */
322 /* Print een volledig scherm af ... */
323 /* ----------------------------------------- */
view_refresh()324 void view_refresh()
325 {
326 int i;
327 if (!p->mode) {if (p->y>p->y_max-1) p->y=p->y_max-1;}
328 else { if (p->y_max>p->lines)
329 if (p->y+p->sy+p->lines>p->y_max-1) p->y=p->y_max-p->lines;
330 }
331
332 werase(p->win);
333
334 for (i=0;i<p->lines;i++) view_addline(i,p->y++);
335 wrefresh(p->win);
336 if (p->mode) {
337 leaveok(p->win,FALSE);
338 curs_set(1);
339 view_set_cursor();
340 }
341 }
342 /* ----------------------------------------------------- */
343 /* Scherm 1 regel naarboven + volgende regel printen ... */
344 /* ----------------------------------------------------- */
view_down()345 void view_down()
346 {
347 if (!p->mode) {
348 if (p->y<(p->y_max+p->lines-1)) {
349 scrollok(p->win,TRUE);
350 wscrl(p->win,1);
351 scrollok(p->win,FALSE);
352 view_addline(p->lines-1,p->y++);
353 wrefresh(p->win);
354 }
355 }
356 else {
357 if (p->y+p->sy-p->lines<p->y_max-1) {
358 if (++p->sy>=p->lines) {
359 p->sy=p->lines-1;
360 scrollok(p->win,TRUE);
361 wscrl(p->win,1);
362 scrollok(p->win,FALSE);
363 view_addline(p->lines-1,p->y++);
364 wrefresh(p->win);
365 }
366 }
367 view_set_cursor();
368 }
369 }
370 /* ----------------------------------------------------- */
371 /* Scherm 1 regel naarbeneden + vorige regel printen ... */
372 /* ----------------------------------------------------- */
view_up()373 void view_up()
374 {
375 if (!p->mode) {
376 if (p->y>p->lines) {
377 scrollok(p->win,TRUE);
378 wscrl(p->win,-1);
379 scrollok(p->win,FALSE);
380 p->y-=p->lines;
381 view_addline(0,--p->y);
382 p->y+=p->lines;
383 wrefresh(p->win);
384 }
385 }
386 else {
387 if (--p->sy<0) {
388 p->sy=0;
389 if (p->y>p->lines) {
390 scrollok(p->win,TRUE);
391 wscrl(p->win,-1);
392 scrollok(p->win,FALSE);
393 p->y-=p->lines;
394 view_addline(0,--p->y);
395 p->y+=p->lines;
396 wrefresh(p->win);
397 }
398 }
399 view_set_cursor();
400 }
401 }
402 /* ---------------------------------------------------- */
403 /* Vorige pagina printen ... */
404 /* ---------------------------------------------------- */
view_previous()405 void view_previous()
406 {
407 if (p->y>=2*(p->lines)) p->y-=2*(p->lines);
408 else p->y=0;
409 view_refresh();
410 }
411 /* ---------------------------------------------------- */
412 /* Scherm naar rechts ... */
413 /* ---------------------------------------------------- */
view_right()414 void view_right()
415 {
416 if (!p->mode) {
417 if (p->x<p->x_max) {
418 ++p->x;
419 p->y-=p->lines;
420 view_refresh();
421 }
422 }
423 else {
424 if (p->x+p->sx<view_strlen(p->file[p->sy+p->y-p->lines])-1) {
425 if (++p->sx>=p->cols) {
426 p->sx=p->cols-7;
427 p->y-=p->lines;
428 p->x+=7;
429 view_refresh();
430 }
431 else view_set_cursor();
432 }
433 }
434 }
435 /* ---------------------------------------------------- */
436 /* Scherm naar links ... */
437 /* ---------------------------------------------------- */
view_left()438 void view_left()
439 {
440 if (!p->mode) {
441 if (p->x>0) {
442 --p->x;
443 p->y-=p->lines;
444 view_refresh();
445 }
446 }
447 else {
448 if(--p->sx<0) {
449 if ((p->x+p->sx)>=0) {
450 p->sx=6;
451 p->y-=p->lines;
452 p->x-=7;
453 if (p->x<0) {
454 p->sx+=p->x;
455 p->x=0;
456 }
457 view_refresh();
458 }
459 else p->sx=0;
460 }
461 else view_set_cursor();
462 }
463 }
464
465
466