source: global.c @ e173507

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since e173507 was e1c4636, checked in by Erik Nygren <nygren@mit.edu>, 22 years ago
* Added owl::send_zwrite(command, message) to the perl glue to allow for the direct sending of multi-line messages. For example: owl::send_zwrite("-c foo -i bar", "hello"); * Reverted attempted fix for the pagedown problem which just made things worse. * Changed owl_fmtext_print_plain to return an alloc'd string to avoid buffer overrun risks. * Added owl::ztext_stylestrip("...") function to perlglue which returns the ztext with formatting stripped out. * Added colorztext variable which can be used to disable @color() strings arriving in messages after it is set. (Currently, changing its value won't reformat messages). * Outgoing zephyr logging now obeys the logpath variable. * The '~' character in logpath and classlogpath now gets replaced with the user's home directory.
  • Property mode set to 100644
File size: 11.9 KB
Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <netdb.h>
6#include <termios.h>
7#include <sys/ioctl.h>
8#include <time.h>
9#include "owl.h"
10
11static const char fileIdent[] = "$Id$";
12
13#ifndef MAXHOSTNAMELEN
14#define MAXHOSTNAMELEN 256
15#endif
16
17void owl_global_init(owl_global *g) {
18  struct hostent *hent;
19  char hostname[MAXHOSTNAMELEN];
20
21  gethostname(hostname, MAXHOSTNAMELEN);
22  hent=gethostbyname(hostname);
23  if (!hent) {
24    strcpy(g->thishost, "localhost");
25  } else {
26    strcpy(g->thishost, hent->h_name);
27  }
28
29  owl_context_init(&g->ctx);
30  owl_context_set_startup(&g->ctx);
31  g->curmsg=0;
32  g->topmsg=0;
33  g->needrefresh=1;
34
35  owl_variable_dict_setup(&(g->vars));
36  owl_cmddict_setup(&(g->cmds));
37
38  g->lines=LINES;
39  g->cols=COLS;
40
41  g->rightshift=0;
42
43  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE, NULL);
44
45  owl_keyhandler_init(&g->kh);
46  owl_keys_setup_keymaps(&g->kh);
47
48  owl_list_create(&(g->filterlist));
49  owl_list_create(&(g->puntlist));
50  g->curmsg_vert_offset=0;
51  g->resizepending=0;
52  g->typwinactive=0;
53  g->direction=OWL_DIRECTION_DOWNWARDS;
54  g->zaway=0;
55  if (has_colors()) {
56    g->hascolors=1;
57  }
58  g->colorpairs=COLOR_PAIRS;
59  g->debug=OWL_DEBUG;
60  g->starttime=time(NULL); /* assumes we call init only a start time */
61  strcpy(g->buffercommand, "");
62
63  owl_global_set_config_format(g, 0);
64  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
65  owl_global_set_no_have_config(g);
66  owl_history_init(&(g->msghist));
67  owl_history_init(&(g->cmdhist));
68  g->nextmsgid=0;
69
70  owl_filterelement_create_true(&(g->fe_true));
71  owl_filterelement_create_false(&(g->fe_false));
72  owl_filterelement_create_null(&(g->fe_null));
73
74  _owl_global_setup_windows(g);
75
76  /* Fill in some variables which don't have constant defaults */
77  /* TODO: come back later and check passwd file first */
78  strcpy(g->homedir, getenv("HOME"));
79
80  owl_messagelist_create(&(g->msglist));
81  owl_mainwin_init(&(g->mw));
82  owl_popwin_init(&(g->pw));
83}
84
85void _owl_global_setup_windows(owl_global *g) {
86  int lines, cols, typwin_lines;
87
88  lines=g->lines;
89  cols=g->cols;
90  typwin_lines = owl_global_get_typwin_lines(g);
91
92  /* set the new window sizes */
93  g->recwinlines=g->lines-(typwin_lines+2);
94
95  /* create the new windows */
96  g->recwin=newwin(g->recwinlines, cols, 0, 0);
97  g->sepwin=newwin(1, cols, g->recwinlines, 0);
98  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
99  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
100
101  owl_editwin_set_curswin(&(g->tw), g->typwin, typwin_lines, g->cols);
102
103  idlok(g->typwin, FALSE);
104  idlok(g->recwin, FALSE);
105  idlok(g->sepwin, FALSE);
106  idlok(g->msgwin, FALSE);
107
108  nodelay(g->typwin, 1);
109  keypad(g->typwin, TRUE);
110  wmove(g->typwin, 0, 0);
111
112  meta(g->typwin, TRUE);
113}
114
115owl_context *owl_global_get_context(owl_global *g) {
116  return(&g->ctx);
117}
118                         
119int owl_global_get_lines(owl_global *g) {
120  return(g->lines);
121}
122
123int owl_global_get_cols(owl_global *g) {
124  return(g->cols);
125}
126
127int owl_global_get_recwin_lines(owl_global *g) {
128  return(g->recwinlines);
129}
130
131/* curmsg */
132
133int owl_global_get_curmsg(owl_global *g) {
134  return(g->curmsg);
135}
136
137void owl_global_set_curmsg(owl_global *g, int i) {
138  g->curmsg=i;
139  /* we will reset the vertical offset from here */
140  /* we might want to move this out to the functions later */
141  owl_global_set_curmsg_vert_offset(g, 0);
142}
143
144/* topmsg */
145
146int owl_global_get_topmsg(owl_global *g) {
147  return(g->topmsg);
148}
149
150void owl_global_set_topmsg(owl_global *g, int i) {
151  g->topmsg=i;
152}
153
154/* windows */
155
156owl_mainwin *owl_global_get_mainwin(owl_global *g) {
157  return(&(g->mw));
158}
159
160owl_popwin *owl_global_get_popwin(owl_global *g) {
161  return(&(g->pw));
162}
163
164/* msglist */
165
166owl_messagelist *owl_global_get_msglist(owl_global *g) {
167  return(&(g->msglist));
168}
169
170/* keyhandler */
171
172owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
173  return(&(g->kh));
174}
175
176/* curses windows */
177
178WINDOW *owl_global_get_curs_recwin(owl_global *g) {
179  return(g->recwin);
180}
181
182WINDOW *owl_global_get_curs_sepwin(owl_global *g) {
183  return(g->sepwin);
184}
185
186WINDOW *owl_global_get_curs_msgwin(owl_global *g) {
187  return(g->msgwin);
188}
189
190WINDOW *owl_global_get_curs_typwin(owl_global *g) {
191  return(g->typwin);
192}
193
194/* typwin */
195
196owl_editwin *owl_global_get_typwin(owl_global *g) {
197  return(&(g->tw));
198}
199
200/* buffercommand */
201
202void owl_global_set_buffercommand(owl_global *g, char *command) {
203  strcpy(g->buffercommand, command);
204}
205
206char *owl_global_get_buffercommand(owl_global *g) {
207  return(g->buffercommand);
208}
209
210/* refresh */
211
212int owl_global_is_needrefresh(owl_global *g) {
213  if (g->needrefresh==1) return(1);
214  return(0);
215}
216
217void owl_global_set_needrefresh(owl_global *g) {
218  g->needrefresh=1;
219}
220
221void owl_global_set_noneedrefresh(owl_global *g) {
222  g->needrefresh=0;
223}
224
225/* variable dictionary */
226
227owl_vardict *owl_global_get_vardict(owl_global *g) {
228  return &(g->vars);
229}
230
231/* command dictionary */
232
233owl_cmddict *owl_global_get_cmddict(owl_global *g) {
234  return &(g->cmds);
235}
236
237/* rightshift */
238
239void owl_global_set_rightshift(owl_global *g, int i) {
240  g->rightshift=i;
241}
242
243int owl_global_get_rightshift(owl_global *g) {
244  return(g->rightshift);
245}
246
247/* typwin */
248
249int owl_global_is_typwin_active(owl_global *g) {
250  if (g->typwinactive==1) return(1);
251  return(0);
252}
253
254void owl_global_set_typwin_active(owl_global *g) {
255  g->typwinactive=1;
256}
257
258void owl_global_set_typwin_inactive(owl_global *g) {
259  g->typwinactive=0;
260}
261
262/* resize */
263
264void owl_global_set_resize_pending(owl_global *g) {
265  g->resizepending=1;
266}
267
268char *owl_global_get_homedir(owl_global *g) {
269  return(g->homedir);
270}
271
272int owl_global_get_direction(owl_global *g) {
273  return(g->direction);
274}
275
276void owl_global_set_direction_downwards(owl_global *g) {
277  g->direction=OWL_DIRECTION_DOWNWARDS;
278}
279
280void owl_global_set_direction_upwards(owl_global *g) {
281  g->direction=OWL_DIRECTION_UPWARDS;
282}
283
284/* perl stuff */
285
286void owl_global_set_perlinterp(owl_global *g, void *p) {
287  g->perl=p;
288}
289
290void *owl_global_get_perlinterp(owl_global *g) {
291  return(g->perl);
292}
293
294int owl_global_is_config_format(owl_global *g) {
295  if (g->config_format) return(1);
296  return(0);
297}
298
299void owl_global_set_config_format(owl_global *g, int state) {
300  if (state==1) {
301    g->config_format=1;
302  } else {
303    g->config_format=0;
304  }
305}
306
307void owl_global_set_have_config(owl_global *g) {
308  g->haveconfig=1;
309}
310
311void owl_global_set_no_have_config(owl_global *g) {
312  g->haveconfig=0;
313}
314
315int owl_global_have_config(owl_global *g) {
316  if (g->haveconfig) return(1);
317  return(0);
318}
319
320void owl_global_resize(owl_global *g, int x, int y) {
321  /* resize the screen.  If x or y is 0 use the terminal size */
322  struct winsize size;
323   
324  if (!g->resizepending) return;
325
326  /* delete the current windows */
327  delwin(g->recwin);
328  delwin(g->sepwin);
329  delwin(g->msgwin);
330  delwin(g->typwin);
331  if (!isendwin()) {
332    endwin();
333  }
334
335  refresh();
336
337  /* get the new size */
338  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
339  if (x==0) {
340    g->lines=size.ws_row;
341  } else {
342    g->lines=x;
343  }
344
345  if (y==0) {
346    g->cols=size.ws_col;
347  } else {
348    g->cols=y;
349  }
350
351  /* resizeterm(size.ws_row, size.ws_col); */
352
353  /* re-initialize the windows */
354  _owl_global_setup_windows(g);
355
356  /* refresh stuff */
357  g->needrefresh=1;
358  owl_mainwin_redisplay(&(g->mw));
359  sepbar(NULL);
360
361  if (owl_global_is_typwin_active(g)) {
362    owl_editwin_redisplay(&(g->tw), 0);
363  }     
364  /* TODO: this should handle other forms of popwins */
365  if (owl_popwin_is_active(owl_global_get_popwin(g)) 
366      && owl_global_get_viewwin(g)) {
367    owl_popwin_refresh(owl_global_get_popwin(g));
368    owl_viewwin_redisplay(owl_global_get_viewwin(g), 0);
369  }
370
371  /*
372  char buff[1024];
373  sprintf(buff, "New size is %i lines, %i cols.\n", size.ws_row, size.ws_col);
374  owl_function_makemsg(buff);
375  */
376  owl_function_makemsg("");
377
378  g->resizepending=0;
379}
380
381/* tty (not fully implemented yet) */
382
383void owl_global_set_tty(owl_global *g, char *tty) {
384  if (tty) {
385    strcpy(g->thistty, tty);
386  } else if (getenv("DISPLAY")) {
387    strcpy(g->thistty, getenv("DISPLAY"));
388  } else if (ttyname(fileno(stdout))) {
389    strcpy(g->thistty, ttyname(fileno(stdout)));
390    if (!strncmp(g->thistty, "/dev/", 5)) {
391      strcpy(g->thistty, g->thistty+5);
392    }
393  } else {
394    strcpy(g->thistty, "unknown");
395  }
396   
397#ifdef HAVE_LIBZEPHYR_ZINITLOCATIONINFO
398  ZInitLocationInfo(g->thishost, g->thistty); 
399#endif
400}
401
402
403/* debug */
404
405int owl_global_is_debug_fast(owl_global *g) {
406  if (g->debug) return(1);
407  return(0);
408}
409
410/* starttime */
411
412time_t owl_global_get_starttime(owl_global *g) {
413  return(g->starttime);
414}
415
416time_t owl_global_get_runtime(owl_global *g) {
417  return(time(NULL)-g->starttime);
418}
419
420void owl_global_get_runtime_string(owl_global *g, char *buff) {
421  time_t diff;
422
423  diff=time(NULL)-owl_global_get_starttime(g);
424
425  /* print something nicer later */   
426  sprintf(buff, "%i seconds", (int) diff);
427}
428
429/* userclue */
430
431void owl_global_set_userclue(owl_global *g, int clue) {
432  g->userclue=clue;
433}
434
435void owl_global_add_userclue(owl_global *g, int clue) {
436  g->userclue|=clue;
437}
438
439int owl_global_get_userclue(owl_global *g) {
440  return(g->userclue);
441}
442
443int owl_global_is_userclue(owl_global *g, int clue) {
444  if (g->userclue & clue) return(1);
445  return(0);
446}
447
448/* viewwin */
449
450owl_viewwin *owl_global_get_viewwin(owl_global *g) {
451  return(&(g->vw));
452}
453
454
455/* vert offset */
456
457int owl_global_get_curmsg_vert_offset(owl_global *g) {
458  return(g->curmsg_vert_offset);
459}
460
461void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
462  g->curmsg_vert_offset=i;
463}
464
465/* startup args */
466
467void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
468  int i;
469
470  strcpy(g->startupargs, "");
471  for (i=0; i<argc; i++) {
472    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
473  }
474  g->startupargs[strlen(g->startupargs)-1]='\0';
475}
476
477char *owl_global_get_startupargs(owl_global *g) {
478  return(g->startupargs);
479}
480
481/* history */
482
483owl_history *owl_global_get_msg_history(owl_global *g) {
484  return(&(g->msghist));
485}
486
487owl_history *owl_global_get_cmd_history(owl_global *g) {
488  return(&(g->cmdhist));
489}
490
491/* filterlist */
492
493owl_list *owl_global_get_filterlist(owl_global *g) {
494  return(&(g->filterlist));
495}
496
497owl_filter *owl_global_get_filter(owl_global *g, char *name) {
498  int i, j;
499  owl_filter *f;
500
501  j=owl_list_get_size(&(g->filterlist));
502  for (i=0; i<j; i++) {
503    f=owl_list_get_element(&(g->filterlist), i);
504    if (!strcmp(name, owl_filter_get_name(f))) {
505      return(f);
506    }
507  }
508  return(NULL);
509}
510
511void owl_global_add_filter(owl_global *g, owl_filter *f) {
512  owl_list_append_element(&(g->filterlist), f);
513}
514
515void owl_global_remove_filter(owl_global *g, char *name) {
516  int i, j;
517  owl_filter *f;
518
519  j=owl_list_get_size(&(g->filterlist));
520  for (i=0; i<j; i++) {
521    f=owl_list_get_element(&(g->filterlist), i);
522    if (!strcmp(name, owl_filter_get_name(f))) {
523      owl_filter_free(f);
524      owl_list_remove_element(&(g->filterlist), i);
525      break;
526    }
527  }
528}
529
530/* nextmsgid */
531
532int owl_global_get_nextmsgid(owl_global *g) {
533  return(g->nextmsgid++);
534}
535
536/* current view */
537
538owl_view *owl_global_get_current_view(owl_global *g) {
539  return(&(g->current_view));
540}
541
542owl_filterelement *owl_global_get_filterelement_true(owl_global *g) {
543  return(&(g->fe_true));
544}
545
546owl_filterelement *owl_global_get_filterelement_false(owl_global *g) {
547  return(&(g->fe_false));
548}
549
550owl_filterelement *owl_global_get_filterelement_null(owl_global *g) {
551  return(&(g->fe_null));
552}
553
554/* has colors */
555
556int owl_global_get_hascolors(owl_global *g) {
557  if (g->hascolors) return(1);
558  return(0);
559}
560
561/* color pairs */
562
563int owl_global_get_colorpairs(owl_global *g) {
564  return(g->colorpairs);
565}
566
567/* puntlist */
568
569owl_list *owl_global_get_puntlist(owl_global *g) {
570  return(&(g->puntlist));
571}
572
573int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
574  owl_list *pl;
575  int i, j;
576
577  pl=owl_global_get_puntlist(g);
578  j=owl_list_get_size(pl);
579  for (i=0; i<j; i++) {
580    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
581  }
582  return(0);
583}
584
585int owl_global_should_followlast(owl_global *g) {
586  owl_view *v;
587 
588  if (!owl_global_is__followlast(g)) return(0);
589 
590  v=owl_global_get_current_view(g);
591 
592  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
593  return(0);
594}
Note: See TracBrowser for help on using the repository browser.