File: wmutil.c

package info (click to toggle)
wmmail 0.63a-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 868 kB
  • ctags: 235
  • sloc: ansic: 2,936; sh: 388; makefile: 144; perl: 111
file content (546 lines) | stat: -rw-r--r-- 9,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 * WMMail - Window Maker Mail
 *
 * wmutil.c: WMMail miscellaneous function library, ripped from
 *           Alfredo Kojima's Window Maker (http://www.windowmaker.org)
 *
 * Window Maker miscellaneous function library
 * 
 * Copyright (c) 1997 Alfredo K. Kojima
 * Modifications Copyright (c) 1998 Bryan Chan
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: wmutil.c,v 1.6 1999/01/16 04:50:25 bryan.chan Exp $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <limits.h>
#include "list.h"


extern int   keep_quiet;  /* defined in global.c */
extern char *app_name;    /* defined in global.c */

static char *get_user_home_dir(char *);
static char *check_file(char *, char *, char *);
static char *next_token(char *, char **);
static void  parse_command(char *, char ***, int *);


#define MAX_ERRORMSG_LENGTH  256

void croak(char *fmt,...)
{
  va_list arg_ptr;
  static char buf[MAX_ERRORMSG_LENGTH];

  if (keep_quiet)
    return;

  va_start(arg_ptr, fmt);

  vsprintf(buf, fmt, arg_ptr);
  strcat(buf, "\n");
  fflush(stdout);
  fputs(app_name, stderr);
  fputs(": ", stderr);
  fputs(buf, stderr);
  fflush(stdout);
  fflush(stderr);

  va_end(arg_ptr);
}


void *wmalloc(size_t size)
{
  void *tmp;
    
  tmp = calloc(1, size);

  if (tmp == NULL)
  {
    croak("wmalloc() failed; retrying after 2 seconds");
    sleep(2);
    tmp = calloc(1, size);

    if (tmp == NULL)
    {
      croak("virtual memory exhausted");
      exit(-1);
    }
  }

  return tmp;
}


void *wrealloc(void *ptr, size_t newsize)
{
  void *nptr;

  if (!ptr)
    nptr = calloc (1, newsize);
  else
    nptr = realloc(ptr, newsize);

  if (nptr==NULL)
  {
    croak("wrealloc() failed");
    return NULL;
  }

  return nptr;
}


inline void wfree(void *ptr)
{
  if (ptr != NULL)
    free(ptr);
}


char *wstrdup(char *str)
{
  if (str == NULL)
    return NULL;

  return strcpy(wmalloc(strlen(str) + 1), str);
}


char *get_home_dir()
{
  char *home = getenv("HOME");
  struct passwd *user;

  if (home)
    return home;
    
  if ( !(user = getpwuid(getuid())) )
  {
    croak("cannot get password entry for UID %i", getuid());
    return "/";
  }

  if (!user->pw_dir)
    return "/";
  else
    return user->pw_dir;
}


static char *get_user_home_dir(char *username)
{
  struct passwd *user;
    
  if ( !(user = getpwnam(username)) )
  {
    croak("cannot get password entry for user %s", username);
    return NULL;
  }

  if (!user->pw_dir)
    return "/";
  else
    return user->pw_dir;
}


char *expand_path(char *path)
{
  char buffer2[PATH_MAX+2];
  char buffer[PATH_MAX+2];
  int i;

  memset(buffer, 0, PATH_MAX+2);
    
  if (*path=='~')
  {
    char *home;
        
    path++;
    if (*path=='/' || *path==0)
    {
      home = get_home_dir();
      strcat(buffer, home);
    }
    else
    {
      int j;

      j = 0;
      while (*path!=0 && *path!='/')
      {
        buffer2[j++] = *path;
        buffer2[j] = 0;
        path++;
      }

      home = get_user_home_dir(buffer2);

      if (!home)
        return NULL;

      strcat(buffer, home);
    }
  }
    
  i = strlen(buffer);

  while (*path!=0)
  {
    char *tmp;
        
    if (*path=='$')
    {
      int j = 0;
      path++;

      /* expand $(HOME) or $HOME style environment variables */
      if (*path=='(')
      {
        path++;

        while (*path!=0 && *path!=')')
        {
            buffer2[j++] = *(path++);
            buffer2[j] = 0;
        }

        if (*path==')')
          path++;

        tmp = getenv(buffer2);

        if (!tmp)
        {
          buffer[i] = 0;
          strcat(buffer, "$(");
          strcat(buffer, buffer2);
          strcat(buffer, ")");
          i += strlen(buffer2)+3;
        }
        else
        {
          strcat(buffer, tmp);
          i += strlen(tmp);
        }
      }
      else
      {
        while (*path!=0 && *path!='/')
        {
          buffer2[j++] = *(path++);
          buffer2[j] = 0;
        }

        tmp = getenv(buffer2);

        if (!tmp)
        {
          strcat(buffer, "$");
          strcat(buffer, buffer2);
          i += strlen(buffer2)+1;
        }
        else
        {
            strcat(buffer, tmp);
            i += strlen(tmp);
        }
      }            
    }
    else
    {
      buffer[i++] = *path;
      path++;
    }
  }
    
  return wstrdup(buffer);
}


char *find_resource(char *resource, char *folders)
{
  char *prefix, *path;

  /* try to find resources in the following locations (in order):
   *
   *   1. $GNUSTEP_USER_ROOT/folders/resource
   *   2. ~/GNUstep/folders/resource
   *   3. $GNUSTEP_LOCAL_ROOT/folders/resource
   *   4. /usr/local/GNUstep/folders/resource
   *   5. $GNUSTEP_SYSTEM_ROOT/folders/resource
   *   6. /usr/GNUstep/folders/resource
   */

  if (prefix = getenv("GNUSTEP_USER_ROOT"))
    if (path = check_file(prefix, folders, resource))
      return path;

  if (get_home_dir())
  {
    prefix = wmalloc(strlen(get_home_dir()) + 9);
    strcpy(prefix, get_home_dir());
    strcat(prefix, "/GNUstep");

    if (path = check_file(prefix, folders, resource))
    {
      wfree(prefix);
      return path;
    }
    else
      wfree(prefix);
  }

  if (prefix = getenv("GNUSTEP_LOCAL_ROOT"))
    if (path = check_file(prefix, folders, resource))
      return path;

#if 0
  if (path = check_file("/usr/local/GNUstep", folders, resource))
    return path;
#endif

  if (prefix = getenv("GNUSTEP_SYSTEM_ROOT"))
    if (path = check_file(prefix, folders, resource))
      return path;

  if (path = check_file("/etc/GNUstep", folders, resource))
    return path;

  return (char *) NULL;
}


static char *check_file(char *path, char *folders, char *resource)
{
  char *buf;
  int   bufsize;

  bufsize = strlen(path) + strlen(resource) + (folders ? strlen(folders) : 0);
  bufsize += 3;    /* account for slashes and terminating NULL */
  buf = (char *) wmalloc(bufsize);

  strcpy(buf, path);

  if (folders)
  {
    strcat(buf, "/");
    strcat(buf, folders);
  }

  strcat(buf, "/");
  strcat(buf, resource);

#ifdef DEBUG
  croak("checking %s", buf);
#endif

  if (access(buf, F_OK) != 0)
  {
    wfree(buf);
    buf = NULL;
  }

  return buf;
}


pid_t exec_command(char *command)
{
  pid_t pid;
  char **argv;
  int argc;

  parse_command(command, &argv, &argc);

  if (argv==NULL)
    return 0;

  if ((pid=fork())==0)
  {
    char **args;
    int i;

#ifdef HAVE_SETPGID
    setpgid(0, 0);
#endif
        
    args = malloc(sizeof(char*)*(argc+1));

    if (!args)
      exit(111);

    for (i=0; i<argc; i++)
      args[i] = argv[i];

    args[argc] = NULL;
    signal (SIGINT, SIG_DFL);
    signal (SIGQUIT, SIG_DFL);
    signal (SIGTSTP, SIG_DFL);
    signal (SIGTTIN, SIG_DFL);
    signal (SIGTTOU, SIG_DFL);
    signal (SIGCHLD, SIG_DFL);
    execvp(argv[0], args);
    exit(111);
  }

  while (argc > 0)
    wfree(argv[--argc]);

  wfree(argv);

  return pid;
}


/* parse_command: divides a command line into a argv/argc pair */

#define PRC_ALPHA        0
#define PRC_BLANK        1
#define PRC_ESCAPE       2
#define PRC_DQUOTE       3
#define PRC_EOS          4
#define PRC_SQUOTE       5

typedef struct {
  short nstate;
  short output;
} DFA;


static DFA mtable[9][6] = {
  {{3,1},{0,0},{4,0},{1,0},{8,0},{6,0}},
  {{1,1},{1,1},{2,0},{3,0},{5,0},{1,1}},
  {{1,1},{1,1},{1,1},{1,1},{5,0},{1,1}},
  {{3,1},{5,0},{4,0},{1,0},{5,0},{6,0}},
  {{3,1},{3,1},{3,1},{3,1},{5,0},{3,1}},
  {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
  {{6,1},{6,1},{7,0},{6,1},{5,0},{3,0}},
  {{6,1},{6,1},{6,1},{6,1},{5,0},{6,1}},
  {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
};


static char* next_token(char *word, char **next)
{
  char *ptr;
  char *ret, *t;
  int state, ctype;

  t = ret = wmalloc(strlen(word)+1);
  ptr = word;
    
  state = 0;
  *t = 0;
  while (1)
  {
    if (*ptr==0) 
        ctype = PRC_EOS;
    else if (*ptr=='\\')
        ctype = PRC_ESCAPE;
    else if (*ptr=='"')
        ctype = PRC_DQUOTE;
    else if (*ptr=='\'')
        ctype = PRC_SQUOTE;
    else if (*ptr==' ' || *ptr=='\t')
        ctype = PRC_BLANK;
    else
        ctype = PRC_ALPHA;

    if (mtable[state][ctype].output)
    {
      *t = *ptr; t++;
      *t = 0;
    }

    state = mtable[state][ctype].nstate;
    ptr++;

    if (mtable[state][0].output<0)
      break;
  }

  if (*ret==0)
    t = NULL;
  else
    t = wstrdup(ret);

  wfree(ret);
    
  if (ctype==PRC_EOS)
    *next = NULL;
  else
    *next = ptr;
    
  return t;
}


static void parse_command(char *command, char ***argv, int *argc)
{
  LinkedList *list = NULL;
  char *token, *line;
  int count, i;

  line = command;

  do
  {
    token = next_token(line, &line);
    if (token)
      list = list_cons(token, list);
  } while (token!=NULL && line!=NULL);

  count = list_length(list);
  *argv = wmalloc(sizeof(char*)*count);
  i = count;

  while (list!=NULL)
  {
    (*argv)[--i] = list->head;
    list_remove_head(&list);
  }

  *argc = count;
}


void unescape(char *s)
{
  int i, j;

  for (i = 0, j = 0; s[j] != '\0'; i++, j++)
  {
    if (s[j] != '\\') 
      s[i] = s[j];
    else 
      s[i] = s[++j];
  } 

  s[i] = '\0';
}