COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
gntp-send.c
Go to the documentation of this file.
1 #ifdef _WIN32
2 #include <winsock2.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 
9 #include "md5.h"
10 #include "tcp.h"
11 #include "growl.h"
12 
13 static char* string_to_utf8_alloc(const char* str) {
14 #ifdef _WIN32
15  unsigned int codepage;
16  size_t in_len = strlen(str);
17  wchar_t* wcsdata;
18  char* mbsdata;
19  size_t mbssize, wcssize;
20 
21  codepage = GetACP();
22  wcssize = MultiByteToWideChar(codepage, 0, str, in_len, NULL, 0);
23  wcsdata = (wchar_t*) malloc((wcssize + 1) * sizeof(wchar_t));
24  wcssize = MultiByteToWideChar(codepage, 0, str, in_len, wcsdata, wcssize + 1);
25  wcsdata[wcssize] = 0;
26 
27  mbssize = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) wcsdata, -1, NULL, 0, NULL, NULL);
28  mbsdata = (char*) malloc((mbssize + 1));
29  mbssize = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) wcsdata, -1, mbsdata, mbssize, NULL, NULL);
30  mbsdata[mbssize] = 0;
31  free(wcsdata);
32  return mbsdata;
33 #else
34  char *ret = malloc (strlen (str) + 1);
35  if(ret !=NULL) strcpy(ret,str);
36  return ret;
37 #endif
38 }
39 
40 int opterr = 1;
41 int optind = 1;
42 int optopt;
43 char *optarg;
44 
45 int getopts(int argc, char** argv, char* opts) {
46  static int sp = 1;
47  register int c;
48  register char *cp;
49 
50  if(sp == 1) {
51  if(optind >= argc ||
52  argv[optind][0] != '-' || argv[optind][1] == '\0')
53  return(EOF);
54  else if(strcmp(argv[optind], "--") == 0) {
55  optind++;
56  return(EOF);
57  }
58  }
59  optopt = c = argv[optind][sp];
60  if(c == ':' || (cp=strchr(opts, c)) == NULL) {
61  if(argv[optind][++sp] == '\0') {
62  optind++;
63  sp = 1;
64  }
65  return('?');
66  }
67  if(*++cp == ':') {
68  if(argv[optind][sp+1] != '\0')
69  optarg = &argv[optind++][sp+1];
70  else if(++optind >= argc) {
71  sp = 1;
72  return('?');
73  } else
74  optarg = argv[optind++];
75  sp = 1;
76  } else {
77  if(argv[optind][++sp] == '\0') {
78  sp = 1;
79  optind++;
80  }
81  optarg = NULL;
82  }
83  return(c);
84 }
85 
86 int main(int argc, char* argv[]) {
87  int c;
88  int rc;
89  char* server = NULL;
90  char* password = NULL;
91  char* appname = "gntp-send";
92  char* notify = "gntp-send notify";
93  char* title = NULL;
94  char* message = NULL;
95  char* icon = NULL;
96  char* url = NULL;
97  int tcpsend = 1;
98 
99  opterr = 0;
100  while ((c = getopts(argc, argv, "a:n:s:p:u") != -1)) {
101  switch (optopt) {
102  case 'a': appname = optarg; break;
103  case 'n': notify = optarg; break;
104  case 's': server = optarg; break;
105  case 'p': password = optarg; break;
106  case 'u': tcpsend = 0; break;
107  case '?': break;
108  default: argc = 0; break;
109  }
110  optarg = NULL;
111  }
112 
113  if ((argc - optind) < 2 || (argc - optind) > 4) {
114  fprintf(stderr, "%s: [-u] [-a APPNAME] [-n NOTIFY] [-s SERVER:PORT] [-p PASSWORD] title message [icon] [url]\n", argv[0]);
115  exit(1);
116  }
117 
118  title = string_to_utf8_alloc((const char*)argv[optind]);
119  message = string_to_utf8_alloc((const char*)argv[optind + 1]);
120  if ((argc - optind) >= 3) icon = string_to_utf8_alloc((const char*)argv[optind + 2]);
121  if ((argc - optind) == 4) url = string_to_utf8_alloc((const char*)argv[optind + 3]);
122 
123  if (!server) server = "127.0.0.1";
124 
125  growl_init();
126  if (tcpsend) {
127  rc = growl(server,appname,notify,title,message,icon,password,url);
128  } else {
129  rc = growl_udp(server,appname,notify,title,message,icon,password,url);
130  }
131  growl_shutdown();
132 
133  if (title) free(title);
134  if (message) free(message);
135  if (icon) free(icon);
136  if (url) free(url);
137 
138  return rc;
139 }
GROWL_EXPORT int growl_udp(const char *const server, const char *const appname, const char *const notify, const char *const title, const char *const message, const char *const icon, const char *const password, const char *url)
Definition: growl.c:350
static char * string_to_utf8_alloc(const char *str)
Definition: gntp-send.c:13
char * optarg
Definition: gntp-send.c:43
int optind
Definition: gntp-send.c:41
int opterr
Definition: gntp-send.c:40
int getopts(int argc, char **argv, char *opts)
Definition: gntp-send.c:45
GROWL_EXPORT int growl_init(void)
Definition: growl.c:30
int optopt
Definition: gntp-send.c:42
int main(int argc, char *argv[])
Definition: gntp-send.c:86
GROWL_EXPORT void growl_shutdown(void)
Definition: growl.c:49
GROWL_EXPORT int growl(const char *const server, const char *const appname, const char *const notify, const char *const title, const char *const message, const char *const icon, const char *const password, const char *url)
Definition: growl.c:201
Send comments to:
COOLFluiD Web Admin