ISIS Logo
UTILITIES
EPICS Utilities
jcaput.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iterator>
4 #include <cstring>
5 #include <cstdlib>
6 #include <sstream>
7 
8 #include <epicsGetopt.h>
9 #include <epicsString.h>
10 
11 #include <shareLib.h>
12 
13 #include "utilities.h"
14 
15 static void usage()
16 {
17  std::cerr << "Usage: jcaput [-h] [-v] [-s#] [-1] [-i] [-w] pv [value1] [value2] [value3]" << std::endl;
18  std::cerr << "Usage: -h this help page" << std::endl;
19  std::cerr << "Usage: -v verbose output" << std::endl;
20  std::cerr << "Usage: -s# use character # to split stdin into an array" << std::endl;
21  std::cerr << "Usage: -1 send one element array rather than a single number/string" << std::endl;
22  std::cerr << "Usage: -i ignore whitespace in the input rather than creating an array" << std::endl;
23  std::cerr << "Usage: -w# set timeout for channel access to #, default 1.0 sec" << std::endl;
24  std::cerr << "Usage: pv required: process variable to send output to" << std::endl;
25  std::cerr << "Usage: [value1] [value2] ... optional: values to form array from (default: stdin)" << std::endl;
26  std::cerr << " " << std::endl;
27  std::cerr << "Usage: if no values are specified, stdin is read and split on the character specified by -s (default: space)" << std::endl;
28  std::cerr << " " << std::endl;
29  std::cerr << "Usage: echo first,second,third|jcaput -v -s, SOME:PROCESS:VARIABLE" << std::endl;
30  std::cerr << "Usage: jcaput -v SOME:PROCESS:VARIABLE first second third" << std::endl;
31  std::cerr << " " << std::endl;
32 }
33 
34 // jcaput pv [value1] [value2] [value3]
35 // if no value, uses stdin
36 int main(int argc, char* argv[])
37 {
38  std::string comp_str, str;
39  std::list<std::string> items;
40  const char* delims = " \t\r\n";
41  int opt;
42  long double waittime = 1.0f;
43  bool single_element = false;
44  bool verbose = false;
45  bool no_whitespace = false;
46  while ((opt = getopt(argc, argv, "1vhiw:s:")) != -1)
47  {
48  switch (opt) {
49  case 'h':
50  usage();
51  return 0;
52 
53  case 'v':
54  verbose = true;
55  break;
56 
57  case 's':
58  delims = strdup(optarg);
59  break;
60 
61  case '1':
62  single_element = true;
63  break;
64 
65  case 'i':
66  no_whitespace = true;
67  break;
68 
69  case 'w':
70  waittime = atof(optarg);
71  break;
72 
73  default:
74  break;
75  }
76  }
77  int nargs = argc - optind;
78  const char* pv = argv[optind];
79  if (nargs < 1)
80  {
81  usage();
82  return -1;
83  }
84  if (nargs == 1)
85  {
86  std::getline(std::cin, str);
87  char* buffer = strdup(str.c_str());
88  char* saveptr;
89  const char* item = epicsStrtok_r(buffer, delims, &saveptr);
90  while( item != NULL )
91  {
92  items.push_back(item);
93  item = epicsStrtok_r(NULL, delims, &saveptr);
94  }
95  }
96  else
97  {
98  if (!no_whitespace)
99  {
100  for(int i=optind+1; i<argc; ++i)
101  {
102  items.push_back(argv[i]);
103  }
104  } else {
105  std::string input = "";
106  for(int i=optind+1; i<argc; ++i)
107  {
108  input.append(argv[i]);
109  input.append(" ");
110  }
111  items.push_back(input);
112  }
113  }
114  std::string json;
115  if (!single_element && (items.size() == 1))
116  {
117  json = "\"" + items.front() + "\"";
118  }
119  else
120  {
121  json = json_list_to_array(items);
122  }
123  int ret = compressString(json, comp_str);
124  if (ret == 0)
125  {
126  std::ostringstream command;
127  command << "caput -S -w" << waittime << " " << pv << " " << comp_str;
128  if (verbose)
129  {
130  std::cout << "jcaput: JSON: " << json << std::endl;
131  std::cout << "jcaput: " << command.str() << std::endl;
132  }
133  system(command.str().c_str());
134  }
135  return ret;
136 }
int main(int argc, char *argv[])
Definition: jcaput.cpp:36
epicsShareFunc std::string json_list_to_array(std::list< std::string > &items)
Definition: json.cpp:8
epicsShareFunc int epicsShareAPI compressString(const std::string &str, std::string &comp_str)
compress a string usinf zlib and then convert compressed bytes into an ascii hex sequence suitable fo...
Definition: compress.cpp:19
static void usage()
Definition: jcaput.cpp:15
Copyright © 2013 Science and Technology Facilities Council | Generated by   doxygen 1.8.5