Switch to unified view

a/src/utils/execmd.cpp b/src/utils/execmd.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.18 2006-10-09 16:37:08 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.19 2006-10-11 16:09:45 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
/*
4
/*
5
 *   This program is free software; you can redistribute it and/or modify
5
 *   This program is free software; you can redistribute it and/or modify
6
 *   it under the terms of the GNU General Public License as published by
6
 *   it under the terms of the GNU General Public License as published by
7
 *   the Free Software Foundation; either version 2 of the License, or
7
 *   the Free Software Foundation; either version 2 of the License, or
...
...
91
    if (pipeout[1] >= 0)
91
    if (pipeout[1] >= 0)
92
        close(pipeout[1]);
92
        close(pipeout[1]);
93
    }
93
    }
94
};
94
};
95
95
96
  
96
97
int ExecCmd::doexec(const string &cmd, const list<string>& args,
97
int ExecCmd::doexec(const string &cmd, const list<string>& args,
98
        const string *inputstring, string *output)
98
        const string *inputstring, string *output)
99
{
99
{
100
    { // Debug and logging
100
    { // Debug and logging
101
    string command = cmd + " ";
101
    string command = cmd + " ";
...
...
179
                errno));
179
                errno));
180
            goto out;
180
            goto out;
181
            }
181
            }
182
            nwritten += n;
182
            nwritten += n;
183
            if (nwritten == inputlen) {
183
            if (nwritten == inputlen) {
184
          // cerr << "Closing output" << endl;
184
          if (m_provide) {
185
              m_provide->newData();
186
              if (inputstring->empty()) {
185
            close(e.pipein[1]);
187
             close(e.pipein[1]);
186
            e.pipein[1] = -1;
188
             e.pipein[1] = -1;
189
              } else {
190
              input = inputstring->data();
191
              inputlen = inputstring->length();
192
              nwritten = 0;
193
              }
194
          } else {
195
              // cerr << "Closing output" << endl;
196
              close(e.pipein[1]);
197
              e.pipein[1] = -1;
198
          }
187
            }
199
            }
188
        }
200
        }
189
        if (e.pipeout[0] > 0 && FD_ISSET(e.pipeout[0], &readfds)) {
201
        if (e.pipeout[0] > 0 && FD_ISSET(e.pipeout[0], &readfds)) {
190
            char buf[8192];
202
            char buf[8192];
191
            int n = read(e.pipeout[0], buf, 8192);
203
            int n = read(e.pipeout[0], buf, 8192);
...
...
298
    }
310
    }
299
    /* This cant be reached: to make cc happy */
311
    /* This cant be reached: to make cc happy */
300
    return -1;
312
    return -1;
301
}
313
}
302
314
315
316
317
////////////////////////////////////////////////////////////////////
303
#else // TEST
318
#else // TEST
304
#include <stdio.h>
319
#include <stdio.h>
305
#include <string>
320
#include <string>
306
#include <iostream>
321
#include <iostream>
307
#include <list>
322
#include <list>
...
...
322
    //  CancelCheck::instance().checkCancel();
337
    //  CancelCheck::instance().checkCancel();
323
    //  cmd->setCancel();
338
    //  cmd->setCancel();
324
    }
339
    }
325
};
340
};
326
341
327
int main(int argc, const char **argv)
342
class MEPv : public ExecCmdProvide {
343
public:
344
    FILE *m_fp;
345
    string *m_input;
346
    MEPv(string *i) 
347
  : m_input(i)
348
    {
349
  m_fp = fopen("/etc/group", "r");
350
    }
351
    ~MEPv() {
352
  if (m_fp)
353
      fclose(m_fp);
354
    }
355
    void newData() {
356
  char line[1024];
357
  if (m_fp && fgets(line, 1024, m_fp)) {
358
      m_input->assign((const char *)line);
359
  } else {
360
      m_input->erase();
361
  }
362
    }
363
};
364
365
366
static char *thisprog;
367
static char usage [] =
368
"execmd cmd [arg1 arg2 ...]\n" 
369
"  \n\n"
370
;
371
static void Usage(void)
328
{
372
{
373
    fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
374
    exit(1);
375
}
376
377
static int     op_flags;
378
#define OPT_MOINS 0x1
379
#define OPT_s   0x2 
380
#define OPT_b   0x4 
381
382
int main(int argc, char **argv)
383
{
384
    int count = 10;
385
    
386
    thisprog = argv[0];
387
    argc--; argv++;
388
389
    while (argc > 0 && **argv == '-') {
390
  (*argv)++;
391
  if (!(**argv))
392
      /* Cas du "adb - core" */
393
      Usage();
394
  while (**argv)
395
      switch (*(*argv)++) {
396
      case 's':   op_flags |= OPT_s; break;
397
      case 'b':   op_flags |= OPT_b; if (argc < 2)  Usage();
398
      if ((sscanf(*(++argv), "%d", &count)) != 1) 
399
          Usage(); 
400
      argc--; 
401
      goto b1;
402
      default: Usage();   break;
403
      }
404
    b1: argc--; argv++;
405
    }
406
407
    if (argc < 1)
408
  Usage();
409
410
    string cmd = *argv++; argc--;
411
    list<string> l;
412
    while (argc > 0) {
413
  l.push_back(*argv++); argc--;
414
    }
415
329
    DebugLog::getdbl()->setloglevel(DEBDEB1);
416
    DebugLog::getdbl()->setloglevel(DEBDEB1);
330
    DebugLog::setfilename("stderr");
417
    DebugLog::setfilename("stderr");
331
    if (argc < 2) {
418
332
  cerr << "Usage: execmd cmd arg1 arg2 ..." << endl;
333
  exit(1);
334
    }
335
    const string cmd = argv[1];
336
    list<string> l;
337
    for (int i = 2; i < argc; i++) {
338
  l.push_back(argv[i]);
339
    }
340
    ExecCmd mexec;
419
    ExecCmd mexec;
341
    MEAdv adv;
420
    MEAdv adv;
342
    adv.cmd = &mexec;
421
    adv.cmd = &mexec;
343
    mexec.setAdvise(&adv);
422
    mexec.setAdvise(&adv);
344
    mexec.setTimeout(500);
423
    mexec.setTimeout(500);
345
    mexec.setStderr("/tmp/trexecStderr");
424
    mexec.setStderr("/tmp/trexecStderr");
346
347
    string input, output;
348
    input = data;
349
    string *ip = 0;
350
    ip = &input;
351
    mexec.putenv("TESTVARIABLE1=TESTVALUE1");
425
    mexec.putenv("TESTVARIABLE1=TESTVALUE1");
352
    mexec.putenv("TESTVARIABLE2=TESTVALUE2");
426
    mexec.putenv("TESTVARIABLE2=TESTVALUE2");
353
    mexec.putenv("TESTVARIABLE3=TESTVALUE3");
427
    mexec.putenv("TESTVARIABLE3=TESTVALUE3");
428
429
    string input, output;
430
    //    input = data;
431
    string *ip = 0;
432
    ip = &input;
433
434
    MEPv  pv(&input);
435
    mexec.setProvide(&pv);
354
436
355
    int status = -1;
437
    int status = -1;
356
    try {
438
    try {
357
    status = mexec.doexec(cmd, l, ip, &output);
439
    status = mexec.doexec(cmd, l, ip, &output);
358
    } catch (CancelExcept) {
440
    } catch (CancelExcept) {
359
    cerr << "CANCELED" << endl;
441
    cerr << "CANCELED" << endl;
360
    }
442
    }
361
443
362
    fprintf(stderr, "Status: 0x%x\n", status);
444
    fprintf(stderr, "Status: 0x%x\n", status);
363
    cout << "Output:" << output << endl;
445
    cout << "Output:[" << output << "]" << endl;
364
    exit (status >> 8);
446
    exit (status >> 8);
365
}
447
}
366
#endif // TEST
448
#endif // TEST