This is Unofficial EPICS BASE Doxygen Site
pvput.cpp File Reference
#include <iostream>
#include <vector>
#include <string>
#include <istream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <epicsExit.h>
#include <epicsStdlib.h>
#include <epicsGetopt.h>
#include <epicsThread.h>
#include <pv/logger.h>
#include <pv/lock.h>
#include <pv/convert.h>
#include <pv/pvdVersion.h>
#include <pv/json.h>
#include <pva/client.h>
#include <pv/pvaDefs.h>
#include <pv/event.h>
#include <pv/caProvider.h>
#include "pvutils.h"
+ Include dependency graph for pvput.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 224 of file pvput.cpp.

225 {
226  try {
227  int opt; /* getopt() current option */
228  bool quiet = false;
229 
230  setvbuf(stdout,NULL,_IOLBF,BUFSIZ); /* Set stdout to line buffering */
231  putenv(const_cast<char*>("POSIXLY_CORRECT=")); /* Behave correct on GNU getopt systems; e.g. handle negative numbers */
232 
233  while ((opt = getopt(argc, argv, ":hvVM:r:w:tp:qdF:f:ns")) != -1) {
234  switch (opt) {
235  case 'h': /* Print usage */
236  usage(true);
237  return 0;
238  case 'v':
239  outmode = pvd::PVStructure::Formatter::Raw;
240  break;
241  case 'V': /* Print version */
242  {
243  fprintf(stdout, "pvAccess %u.%u.%u%s\n",
244  EPICS_PVA_MAJOR_VERSION,
245  EPICS_PVA_MINOR_VERSION,
246  EPICS_PVA_MAINTENANCE_VERSION,
247  (EPICS_PVA_DEVELOPMENT_FLAG)?"-SNAPSHOT":"");
248  fprintf(stdout, "pvData %u.%u.%u%s\n",
249  EPICS_PVD_MAJOR_VERSION,
250  EPICS_PVD_MINOR_VERSION,
251  EPICS_PVD_MAINTENANCE_VERSION,
252  (EPICS_PVD_DEVELOPMENT_FLAG)?"-SNAPSHOT":"");
253  fprintf(stdout, "Base %s\n", EPICS_VERSION_FULL);
254  return 0;
255  }
256  case 'M':
257  if(strcmp(optarg, "raw")==0) {
258  outmode = pvd::PVStructure::Formatter::Raw;
259  } else if(strcmp(optarg, "nt")==0) {
260  outmode = pvd::PVStructure::Formatter::NT;
261  } else if(strcmp(optarg, "json")==0) {
262  outmode = pvd::PVStructure::Formatter::JSON;
263  } else {
264  fprintf(stderr, "Unknown output mode '%s'\n", optarg);
265  outmode = pvd::PVStructure::Formatter::Raw;
266  }
267  break;
268  case 'w': /* Set PVA timeout value */
269  {
270  double temp;
271  if((epicsScanDouble(optarg, &temp)) != 1)
272  {
273  fprintf(stderr, "'%s' is not a valid timeout value "
274  "- ignored. ('pvput -h' for help.)\n", optarg);
275  } else {
276  timeout = temp;
277  }
278  }
279  break;
280  case 'r': /* Set PVA timeout value */
281  request = optarg;
282  break;
283  case 't': /* Terse mode */
284  // deprecated
285  break;
286  case 'd': /* Debug log level */
287  debugFlag = true;
288  break;
289  case 'p': /* Set default provider */
291  break;
292  case 'q': /* Quiet mode */
293  quiet = true;
294  break;
295  case 'F': /* Store this for output formatting */
296  break;
297  case 'f': /* Use input stream as input */
298  fprintf(stderr, "Unsupported option -f\n");
299  return 1;
300  case 'n':
301  break;
302  case 's':
303  break;
304  case '?':
305  fprintf(stderr,
306  "Unrecognized option: '-%c'. ('pvput -h' for help.)\n",
307  optopt);
308  return 1;
309  case ':':
310  fprintf(stderr,
311  "Option '-%c' requires an argument. ('pvput -h' for help.)\n",
312  optopt);
313  return 1;
314  default :
315  usage();
316  return 1;
317  }
318  }
319 
320  if (argc <= optind)
321  {
322  fprintf(stderr, "No pv name specified. ('pvput -h' for help.)\n");
323  return 1;
324  }
325  std::string pv = argv[optind++];
326 
327  std::string providerName(defaultProvider);
328  std::string pvName(pv);
329 
330  int nVals = argc - optind; /* Remaining arg list are PV names */
331  if (nVals < 1)
332  {
333  fprintf(stderr, "No value(s) specified. ('pvput -h' for help.)\n");
334  return 1;
335  }
336 
337  std::vector<std::string> values;
338  // copy values from command line
339  for (int n = 0; optind < argc; n++, optind++)
340  values.push_back(argv[optind]);
341 
342  Putter thework;
343 
344  for(size_t i=0, N=values.size(); i<N; i++)
345  {
346  size_t sep = values[i].find_first_of('=');
347  if(sep==std::string::npos) {
348  thework.bare.push_back(values[i]);
349  } else {
350  thework.pairs.push_back(std::make_pair(values[i].substr(0, sep),
351  values[i].substr(sep+1)));
352  }
353  }
354 
355  if(!thework.bare.empty() && !thework.pairs.empty()) {
356  usage();
357  fprintf(stderr, "\nCan't mix bare values and field=value pairs\n");
358  return 1;
359 
360  } else if(thework.bare.size()==1 && thework.bare[0][0]=='[') {
361  // treat plain "[...]" as "value=[...]"
362  thework.pairs.push_back(std::make_pair("value", thework.bare[0]));
363  thework.bare.clear();
364  }
365 
366  pvd::PVStructure::shared_pointer pvRequest;
367  try {
368  pvRequest = pvd::createRequest(request);
369  } catch(std::exception& e){
370  fprintf(stderr, "failed to parse request string: %s\n", e.what());
371  return 1;
372  }
373 
375 
376  std::cout << std::boolalpha;
377 
379 
380  pvac::ClientProvider ctxt(providerName);
381 
382  pvac::ClientChannel chan(ctxt.connect(pvName));
383 
384  if (!quiet) {
385  std::cout << "Old : ";
386  printValue(pvName, chan.get(timeout, pvRequest));
387  }
388 
389  {
390  pvac::Operation op(chan.put(&thework, pvRequest, true));
391 
392  epicsGuard<epicsMutex> G(thework.lock);
393  while(!thework.done) {
395  if(!thework.wait.wait(timeout)) {
396  fprintf(stderr, "Put timeout\n");
397  return 1;
398  }
399  }
400  }
401 
402  if(thework.result==pvac::PutEvent::Fail) {
403  fprintf(stderr, "Error: %s\n", thework.message.c_str());
404  }
405 
406  if (!quiet) {
407  std::cout << "New : ";
408  }
409  printValue(pvName, chan.get(timeout, pvRequest));
410 
411  return thework.result!=pvac::PutEvent::Success;
412  } catch(std::exception& e) {
413  std::cerr<<"Error: "<<e.what()<<"\n";
414  return 1;
415  }
416 }
double timeout
Definition: pvutils.cpp:25
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: epicsGetopt.c:65
std::string request
int i
Definition: scan.c:967
int optind
Definition: epicsGetopt.c:50
Definition: tool_lib.h:67
#define SET_LOG_LEVEL(level)
Definition: logger.h:50
#define NULL
Definition: catime.c:38
#define epicsScanDouble(str, to)
Definition: epicsStdlib.h:78
PVStructure::shared_pointer createRequest(std::string const &request)
Handle for in-progress get/put/rpc operation.
Definition: client.h:50
int optopt
Definition: epicsGetopt.c:50
int putenv(char *)
#define stdout
Definition: epicsStdio.h:30
Central client context.
Definition: client.h:517
request ends in failure. Check message
Definition: client.h:91
#define stderr
Definition: epicsStdio.h:32
ChannelPut::shared_pointer op
Definition: pvAccess.cpp:132
bool debugFlag
Definition: pvutils.cpp:26
static void start()
start provider ca
Definition: caProvider.cpp:201
std::string defaultProvider
char * optarg
Definition: epicsGetopt.c:55
void usage(void)
Definition: cainfo.c:36
pvd::PVStructure::Formatter::format_t outmode
Definition: pvutils.cpp:28
It worked!
Definition: client.h:93