aboutsummaryrefslogtreecommitdiff
path: root/getenv.c
blob: dbd31a5324a9d4fbd2cd3cf951983078560852b3 (plain)
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
/*************************************************************************** 
 * File: shuffle.c 
 * Auth: Iain Mott [iain.mott@bigpond.com] 
 * Maintainer: Iain Mott [iain.mott@bigpond.com] 
 * Version: Part of motex_1.1.2 
 * Date: January 2001
 * 
 * Description: Pd external. Sends value of an environment variable argument on bang.
 * Use a 'set <NAME>' message to reset the variable name.
 * See supporting Pd patch: getenv.pd
 * 
 * Copyright (C) 2001 by Iain Mott [iain.mott@bigpond.com] 
 * 
 * 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, 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, which should be included with this 
 * program, for more details. 
 * 
 ****************************************************************************/ 

/*   
 * getenv - Pd external. Copyright (c) 2001 Iain Mott 
 * sends value of an environment variable argument on bang
 * use a 'set <NAME>' message to reset the variable name
*/

#include "m_pd.h"
#include <stdlib.h>

static t_class *getenv_class;

typedef struct _getenv
{
  t_object x_obj;
  t_symbol *enval;
  char *envar;
} t_getenv;

static void *getenv_new(t_symbol *s, int argc, t_atom *argv)
{
  
  t_getenv *x = (t_getenv *)pd_new(getenv_class);
  x->envar = NULL;
  if(argc != 1 || argv[0].a_type != A_SYMBOL)
    post("getenv: One argument (environment variable) required");
  else
    {
      char *value = getenv(argv[0].a_w.w_symbol->s_name);
      if(value)
	{
	  x->envar = argv[0].a_w.w_symbol->s_name;
	  x->enval = gensym(value);
	}
      else
	post("getenv: Environment variable does not exist");
    }  
  outlet_new(&x->x_obj, &s_symbol);
  return (x);
}


void getenv_set(t_getenv *x, t_symbol *f)
{
  char *value = getenv(f->s_name);
  if(value)
    x->enval = gensym(value);
  else
    post("getenv: Environment variable does not exist");
}

void getenv_bang(t_getenv *x)
{
  if(x->envar)
    outlet_symbol(x->x_obj.ob_outlet, x->enval);
}

void getenv_setup(void)
{
  getenv_class = class_new(gensym("getenv"), (t_newmethod)getenv_new, 0, sizeof(t_getenv), 0, A_GIMME, 0);
  class_addmethod(getenv_class, (t_method)getenv_set, gensym("set"), A_SYMBOL, 0);  
  class_addbang(getenv_class, getenv_bang);
}