source: util.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Initial check in
  • Property mode set to 100644
File size: 8.7 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4#include <malloc.h>
5#include <ctype.h>
6
7void sepbar(char *in) {
8  char buff[1024];
9  WINDOW *sepwin;
10  owl_messagelist *ml;
11  owl_view *v;
12  int x, y, i;
13  char *foo, *appendtosepbar;
14
15  sepwin=owl_global_get_curs_sepwin(&g);
16  ml=owl_global_get_msglist(&g);
17  v=owl_global_get_current_view(&g);
18
19  werase(sepwin);
20  wattron(sepwin, A_REVERSE);
21  whline(sepwin, ACS_HLINE, owl_global_get_cols(&g));
22
23  wmove(sepwin, 0, 2); 
24
25  if (owl_messagelist_get_size(ml)==0) {
26    strcpy(buff, " (-/-) ");
27  } else {
28    sprintf(buff, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1,
29            owl_view_get_size(v),
30            owl_messagelist_get_size(ml));
31  }
32  waddstr(sepwin, buff);
33
34  foo=owl_view_get_filtname(v);
35  if (strcmp(foo, "all")) wattroff(sepwin, A_REVERSE);
36  waddstr(sepwin, " ");
37  waddstr(sepwin, owl_view_get_filtname(v));
38  waddstr(sepwin, " ");
39  if (strcmp(foo, "all")) wattron(sepwin, A_REVERSE);
40
41  if (owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
42    getyx(sepwin, y, x);
43    wmove(sepwin, y, x+2);
44    wattron(sepwin, A_BOLD);
45    waddstr(sepwin, " <truncated> ");
46    wattroff(sepwin, A_BOLD);
47  }
48
49  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
50  if ((i != -1) &&
51      (i < owl_view_get_size(v)-1)) {
52    getyx(sepwin, y, x);
53    wmove(sepwin, y, x+2);
54    wattron(sepwin, A_BOLD);
55    waddstr(sepwin, " <more> ");
56    wattroff(sepwin, A_BOLD);
57  }
58
59  if (owl_global_get_rightshift(&g)>0) {
60    getyx(sepwin, y, x);
61    wmove(sepwin, y, x+2);
62    sprintf(buff, " right: %i ", owl_global_get_rightshift(&g));
63    waddstr(sepwin, buff);
64  }
65
66  if (owl_global_is_zaway(&g)) {
67    getyx(sepwin, y, x);
68    wmove(sepwin, y, x+2);
69    wattron(sepwin, A_BOLD);
70    wattroff(sepwin, A_REVERSE);
71    waddstr(sepwin, " ZAWAY ");
72    wattron(sepwin, A_REVERSE);
73    wattroff(sepwin, A_BOLD);
74  }
75
76  if (owl_global_get_curmsg_vert_offset(&g)) {
77    getyx(sepwin, y, x);
78    wmove(sepwin, y, x+2);
79    wattron(sepwin, A_BOLD);
80    wattroff(sepwin, A_REVERSE);
81    waddstr(sepwin, " SCROLL ");
82    wattron(sepwin, A_REVERSE);
83    wattroff(sepwin, A_BOLD);
84  }
85 
86  if (in) {
87    getyx(sepwin, y, x);
88    wmove(sepwin, y, x+2);
89    waddstr(sepwin, in);
90  }
91
92  appendtosepbar = owl_global_get_appendtosepbar(&g);
93  if (appendtosepbar && *appendtosepbar) {
94    getyx(sepwin, y, x);
95    wmove(sepwin, y, x+2);
96    waddstr(sepwin, " ");
97    waddstr(sepwin, owl_global_get_appendtosepbar(&g));
98    waddstr(sepwin, " ");
99  }
100
101  getyx(sepwin, y, x);
102  wmove(sepwin, y, owl_global_get_cols(&g)-1);
103   
104  wattroff(sepwin, A_BOLD);
105  wattroff(sepwin, A_REVERSE);
106  wnoutrefresh(sepwin);
107}
108
109
110void pophandler_quit(int ch) {
111  if (ch=='q') {
112    owl_popwin_close(owl_global_get_popwin(&g));
113  }
114}
115
116char **atokenize(char *buffer, char *sep, int *i) {
117  /* each element of return must be freed by user */
118  char **args;
119  char *workbuff, *foo;
120  int done=0, first=1, count=0;
121
122  workbuff=owl_malloc(strlen(buffer)+1);
123  memcpy(workbuff, buffer, strlen(buffer)+1);
124
125  args=NULL;
126  while (!done) {
127    if (first) {
128      first=0;
129      foo=(char *)strtok(workbuff, sep);
130    } else {
131      foo=(char *)strtok(NULL, sep);
132    }
133    if (foo==NULL) {
134      done=1;
135    } else {
136      args=(char **)owl_realloc(args, sizeof(char *) * (count+1));
137      args[count]=owl_malloc(strlen(foo)+1);
138      strcpy(args[count], foo);
139      count++;
140    }
141  }
142  *i=count;
143  owl_free(workbuff);
144  return(args);
145}
146
147/* skips n tokens and returns where that would be.
148 * TODO: handle quotes. */
149char *skiptokens(char *buff, int n) {
150  int inquotes=0;
151  while (*buff && n>0) {
152      while (*buff == ' ') buff++;
153      while (*buff && (inquotes || *buff != ' ')) { 
154        if (*buff == '"') inquotes=!inquotes;
155        buff++;
156      }
157      while (*buff == ' ') buff++;
158      n--;
159  }
160  return buff;
161}
162
163void atokenize_free(char **tok, int nels) {
164  int i;
165  for (i=0; i<nels; i++) {
166    owl_free(tok[i]);
167  }
168  owl_free(tok);
169}
170
171
172void owl_parsefree(char **argv, int argc) {
173  int i;
174
175  if (!argv) return;
176 
177  for (i=0; i<argc; i++) {
178    if (argv[i]) owl_free(argv[i]);
179  }
180  owl_free(argv);
181}
182
183char **owl_parseline(char *line, int *argc) {
184  /* break a command line up into argv, argc.  The caller must free
185     the returned values.  If there is an error argc will be set to
186     -1, argv will be NULL and the caller does not need to free
187     anything */
188
189  char **argv;
190  int i, len, between=1;
191  char *curarg;
192  char quote;
193
194  argv=owl_malloc(sizeof(char *));
195  len=strlen(line);
196  curarg=owl_malloc(len+10);
197  strcpy(curarg, "");
198  quote='\0';
199  *argc=0;
200  for (i=0; i<len+1; i++) {
201    /* find the first real character */
202    if (between) {
203      if (line[i]==' ' || line[i]=='\t' || line[i]=='\0') {
204        continue;
205      } else {
206        between=0;
207        i--;
208        continue;
209      }
210    }
211
212    /* deal with a quote character */
213    if (line[i]=='"' || line[i]=="'"[0]) {
214      /* if this type of quote is open, close it */
215      if (quote==line[i]) {
216        quote='\0';
217        continue;
218      }
219
220      /* if no quoting is open then open with this */
221      if (quote=='\0') {
222        quote=line[i];
223        continue;
224      }
225
226      /* if another type of quote is open then treat this as a literal */
227      curarg[strlen(curarg)+1]='\0';
228      curarg[strlen(curarg)]=line[i];
229      continue;
230    }
231
232    /* if it's not a space or end of command, then use it */
233    if (line[i]!=' ' && line[i]!='\t' && line[i]!='\n' && line[i]!='\0') {
234      curarg[strlen(curarg)+1]='\0';
235      curarg[strlen(curarg)]=line[i];
236      continue;
237    }
238
239    /* otherwise, if we're not in quotes, add the whole argument */
240    if (quote=='\0') {
241      /* add the argument */
242      argv=owl_realloc(argv, sizeof(char *)*((*argc)+1));
243      argv[*argc]=owl_malloc(strlen(curarg)+2);
244      strcpy(argv[*argc], curarg);
245      *argc=*argc+1;
246      strcpy(curarg, "");
247      between=1;
248      continue;
249    }
250
251    /* if it is a space and we're in quotes, then use it */
252    curarg[strlen(curarg)+1]='\0';
253    curarg[strlen(curarg)]=line[i];
254  }
255
256  /* check for unbalanced quotes */
257  if (quote!='\0') {
258    owl_parsefree(argv, *argc);
259    *argc=-1;
260    return(NULL);
261  }
262
263  return(argv);
264}
265
266
267
268int owl_util_find_trans(char *in, int len) {
269  /* return the index of the last char before a change from the first
270     one */
271  int i;
272  for (i=1; i<len; i++) {
273    if (in[i] != in[0]) return(i-1);
274  }
275  return(i);
276}
277
278
279void downstr(char *foo) {
280  int i;
281  for (i=0; foo[i]!='\0'; i++) {
282    foo[i]=tolower(foo[i]);
283  }
284}
285
286char *stristr(char *a, char *b) {
287  char *x, *y, *ret;
288
289  if ((x=owl_strdup(a))==NULL) return(NULL);
290  if ((y=owl_strdup(b))==NULL) return(NULL);
291  downstr(x);
292  downstr(y);
293  ret=strstr(x, y);
294  owl_free(x);
295  owl_free(y);
296  return(ret);
297}
298
299
300void *owl_malloc(size_t size) {
301  return(malloc(size));
302}
303
304void owl_free(void *ptr) {
305  free(ptr);
306}
307
308char *owl_strdup(const char *s1) {
309  return(strdup(s1));
310}
311
312void *owl_realloc(void *ptr, size_t size) {
313  return(realloc(ptr, size));
314}
315
316char *pretty_sender(char *in) {
317  char *out, *ptr;
318 
319  /* the caller must free the return */
320  out=owl_strdup(in);
321  ptr=strchr(out, '@');
322  if (ptr) {
323    if (!strcasecmp(ptr+1, ZGetRealm())) {
324      *ptr='\0';
325    }
326  }
327  return(out);
328}
329
330char *long_sender(char *in) {
331  char *out, *ptr;
332
333  /* the caller must free the return */
334  out=owl_malloc(strlen(in)+100);
335  strcpy(out, in);
336  ptr=strchr(out, '@');
337  if (ptr) return(out);
338  sprintf(out, "%s@%s", out, ZGetRealm());
339  return(out);
340}
341                 
342
343char *owl_getquoting(char *line) {
344  if (line[0]=='\0') return("'");
345  if (strchr(line, '\'')) return("\"");
346  if (strchr(line, '"')) return("'");
347  if (strchr(line, ' ')) return("'");
348  return("");
349}
350
351
352int owl_util_string_to_color(char *color) {
353  if (!strcasecmp(color, "black")) {
354    return(OWL_COLOR_BLACK);
355  } else if (!strcasecmp(color, "red")) {
356    return(OWL_COLOR_RED);
357  } else if (!strcasecmp(color, "green")) {
358    return(OWL_COLOR_GREEN);
359  } else if (!strcasecmp(color, "yellow")) {
360    return(OWL_COLOR_YELLOW);
361  } else if (!strcasecmp(color, "blue")) {
362    return(OWL_COLOR_BLUE);
363  } else if (!strcasecmp(color, "magenta")) {
364    return(OWL_COLOR_MAGENTA);
365  } else if (!strcasecmp(color, "cyan")) {
366    return(OWL_COLOR_CYAN);
367  } else if (!strcasecmp(color, "white")) {
368    return(OWL_COLOR_WHITE);
369  } else if (!strcasecmp(color, "default")) {
370    return(OWL_COLOR_DEFAULT);
371  }
372  return(OWL_COLOR_DEFAULT);
373}
374
375char *owl_util_color_to_string(int color) {
376  if (color==OWL_COLOR_BLACK)   return("black");
377  if (color==OWL_COLOR_RED)     return("red");
378  if (color==OWL_COLOR_GREEN)   return("green");
379  if (color==OWL_COLOR_YELLOW)  return("yellow");
380  if (color==OWL_COLOR_BLUE)    return("blue");
381  if (color==OWL_COLOR_MAGENTA) return("magenta");
382  if (color==OWL_COLOR_CYAN)    return("cyan");
383  if (color==OWL_COLOR_WHITE)   return("white");
384  if (color==OWL_COLOR_DEFAULT) return("default");
385  return("Unknown color");
386}
Note: See TracBrowser for help on using the repository browser.