author | sandman <sandman> | 2002-12-17 19:12:05 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-12-17 19:12:05 (UTC) |
commit | b6a03145553d7f536b04fc3355718cfdd72c590d (patch) (unidiff) | |
tree | cfadab49e69801a2000001f80d57ec081b2c5ea4 /libopie/oprocess.cpp | |
parent | 6bbe59222c6b439f2016d0a36a111f17d338d120 (diff) | |
download | opie-b6a03145553d7f536b04fc3355718cfdd72c590d.zip opie-b6a03145553d7f536b04fc3355718cfdd72c590d.tar.gz opie-b6a03145553d7f536b04fc3355718cfdd72c590d.tar.bz2 |
- removed a the obsolete KShellProcess class
- added a few methods to OProcess to make it easier to port programs using
QProcess
-rw-r--r-- | libopie/oprocess.cpp | 94 |
1 files changed, 49 insertions, 45 deletions
diff --git a/libopie/oprocess.cpp b/libopie/oprocess.cpp index f3e52bd..5db2b6c 100644 --- a/libopie/oprocess.cpp +++ b/libopie/oprocess.cpp | |||
@@ -65,80 +65,100 @@ | |||
65 | #include <stdlib.h> | 65 | #include <stdlib.h> |
66 | #include <signal.h> | 66 | #include <signal.h> |
67 | #include <stdio.h> | 67 | #include <stdio.h> |
68 | #include <string.h> | 68 | #include <string.h> |
69 | #include <unistd.h> | 69 | #include <unistd.h> |
70 | #ifdef HAVE_SYS_SELECT_H | 70 | #ifdef HAVE_SYS_SELECT_H |
71 | #include <sys/select.h> | 71 | #include <sys/select.h> |
72 | #endif | 72 | #endif |
73 | #ifdef HAVE_INITGROUPS | 73 | #ifdef HAVE_INITGROUPS |
74 | #include <grp.h> | 74 | #include <grp.h> |
75 | #endif | 75 | #endif |
76 | #include <pwd.h> | 76 | #include <pwd.h> |
77 | 77 | ||
78 | #include <qapplication.h> | 78 | #include <qapplication.h> |
79 | #include <qmap.h> | 79 | #include <qmap.h> |
80 | //#include <kdebug.h> | 80 | //#include <kdebug.h> |
81 | 81 | ||
82 | ///////////////////////////// | 82 | ///////////////////////////// |
83 | // public member functions // | 83 | // public member functions // |
84 | ///////////////////////////// | 84 | ///////////////////////////// |
85 | 85 | ||
86 | class OProcessPrivate { | 86 | class OProcessPrivate { |
87 | public: | 87 | public: |
88 | OProcessPrivate() : useShell(false) { } | 88 | OProcessPrivate() : useShell(false) { } |
89 | 89 | ||
90 | bool useShell; | 90 | bool useShell; |
91 | QMap<QString,QString> env; | 91 | QMap<QString,QString> env; |
92 | QString wd; | 92 | QString wd; |
93 | QCString shell; | 93 | QCString shell; |
94 | }; | 94 | }; |
95 | 95 | ||
96 | 96 | ||
97 | OProcess::OProcess() | 97 | OProcess::OProcess(QObject *parent, const char *name) |
98 | : QObject(), | 98 | : QObject(parent, name) |
99 | run_mode(NotifyOnExit), | ||
100 | runs(false), | ||
101 | pid_(0), | ||
102 | status(0), | ||
103 | keepPrivs(false), | ||
104 | innot(0), | ||
105 | outnot(0), | ||
106 | errnot(0), | ||
107 | communication(NoCommunication), | ||
108 | input_data(0), | ||
109 | input_sent(0), | ||
110 | input_total(0), | ||
111 | d(0) | ||
112 | { | 99 | { |
100 | init ( ); | ||
101 | } | ||
102 | |||
103 | OProcess::OProcess(const QString &arg0, QObject *parent, const char *name) | ||
104 | : QObject(parent, name) | ||
105 | { | ||
106 | init ( ); | ||
107 | *this << arg0; | ||
108 | } | ||
109 | |||
110 | OProcess::OProcess(const QStringList &args, QObject *parent, const char *name) | ||
111 | : QObject(parent, name) | ||
112 | { | ||
113 | init ( ); | ||
114 | *this << args; | ||
115 | } | ||
116 | |||
117 | void OProcess::init ( ) | ||
118 | { | ||
119 | run_mode = NotifyOnExit; | ||
120 | runs = false; | ||
121 | pid_ = 0; | ||
122 | status = 0; | ||
123 | keepPrivs = false; | ||
124 | innot = 0; | ||
125 | outnot = 0; | ||
126 | errnot = 0; | ||
127 | communication = NoCommunication; | ||
128 | input_data = 0; | ||
129 | input_sent = 0; | ||
130 | input_total = 0; | ||
131 | d = 0; | ||
132 | |||
113 | if (0 == OProcessController::theOProcessController) { | 133 | if (0 == OProcessController::theOProcessController) { |
114 | (void) new OProcessController(); | 134 | (void) new OProcessController(); |
115 | CHECK_PTR(OProcessController::theOProcessController); | 135 | CHECK_PTR(OProcessController::theOProcessController); |
116 | } | 136 | } |
117 | 137 | ||
118 | OProcessController::theOProcessController->addOProcess(this); | 138 | OProcessController::theOProcessController->addOProcess(this); |
119 | out[0] = out[1] = -1; | 139 | out[0] = out[1] = -1; |
120 | in[0] = in[1] = -1; | 140 | in[0] = in[1] = -1; |
121 | err[0] = err[1] = -1; | 141 | err[0] = err[1] = -1; |
122 | } | 142 | } |
123 | 143 | ||
124 | void | 144 | void |
125 | OProcess::setEnvironment(const QString &name, const QString &value) | 145 | OProcess::setEnvironment(const QString &name, const QString &value) |
126 | { | 146 | { |
127 | if (!d) | 147 | if (!d) |
128 | d = new OProcessPrivate; | 148 | d = new OProcessPrivate; |
129 | d->env.insert(name, value); | 149 | d->env.insert(name, value); |
130 | } | 150 | } |
131 | 151 | ||
132 | void | 152 | void |
133 | OProcess::setWorkingDirectory(const QString &dir) | 153 | OProcess::setWorkingDirectory(const QString &dir) |
134 | { | 154 | { |
135 | if (!d) | 155 | if (!d) |
136 | d = new OProcessPrivate; | 156 | d = new OProcessPrivate; |
137 | d->wd = dir; | 157 | d->wd = dir; |
138 | } | 158 | } |
139 | 159 | ||
140 | void | 160 | void |
141 | OProcess::setupEnvironment() | 161 | OProcess::setupEnvironment() |
142 | { | 162 | { |
143 | if (d) | 163 | if (d) |
144 | { | 164 | { |
@@ -443,64 +463,78 @@ bool OProcess::normalExit() const | |||
443 | 463 | ||
444 | 464 | ||
445 | int OProcess::exitStatus() const | 465 | int OProcess::exitStatus() const |
446 | { | 466 | { |
447 | int _status = status; | 467 | int _status = status; |
448 | return WEXITSTATUS((_status)); | 468 | return WEXITSTATUS((_status)); |
449 | } | 469 | } |
450 | 470 | ||
451 | 471 | ||
452 | 472 | ||
453 | bool OProcess::writeStdin(const char *buffer, int buflen) | 473 | bool OProcess::writeStdin(const char *buffer, int buflen) |
454 | { | 474 | { |
455 | bool rv; | 475 | bool rv; |
456 | 476 | ||
457 | // if there is still data pending, writing new data | 477 | // if there is still data pending, writing new data |
458 | // to stdout is not allowed (since it could also confuse | 478 | // to stdout is not allowed (since it could also confuse |
459 | // kprocess... | 479 | // kprocess... |
460 | if (0 != input_data) | 480 | if (0 != input_data) |
461 | return false; | 481 | return false; |
462 | 482 | ||
463 | if (runs && (communication & Stdin)) { | 483 | if (runs && (communication & Stdin)) { |
464 | input_data = buffer; | 484 | input_data = buffer; |
465 | input_sent = 0; | 485 | input_sent = 0; |
466 | input_total = buflen; | 486 | input_total = buflen; |
467 | slotSendData(0); | 487 | slotSendData(0); |
468 | innot->setEnabled(true); | 488 | innot->setEnabled(true); |
469 | rv = true; | 489 | rv = true; |
470 | } else | 490 | } else |
471 | rv = false; | 491 | rv = false; |
472 | return rv; | 492 | return rv; |
473 | } | 493 | } |
474 | 494 | ||
495 | void OProcess::flushStdin ( ) | ||
496 | { | ||
497 | if ( !input_data || ( input_sent == input_total )) | ||
498 | return; | ||
499 | |||
500 | int d1, d2; | ||
501 | |||
502 | do { | ||
503 | d1 = input_total - input_sent; | ||
504 | slotSendData ( 0 ); | ||
505 | d2 = input_total - input_sent; | ||
506 | } while ( d2 <= d1 ); | ||
507 | } | ||
508 | |||
475 | void OProcess::suspend() | 509 | void OProcess::suspend() |
476 | { | 510 | { |
477 | if ((communication & Stdout) && outnot) | 511 | if ((communication & Stdout) && outnot) |
478 | outnot->setEnabled(false); | 512 | outnot->setEnabled(false); |
479 | } | 513 | } |
480 | 514 | ||
481 | void OProcess::resume() | 515 | void OProcess::resume() |
482 | { | 516 | { |
483 | if ((communication & Stdout) && outnot) | 517 | if ((communication & Stdout) && outnot) |
484 | outnot->setEnabled(true); | 518 | outnot->setEnabled(true); |
485 | } | 519 | } |
486 | 520 | ||
487 | bool OProcess::closeStdin() | 521 | bool OProcess::closeStdin() |
488 | { | 522 | { |
489 | bool rv; | 523 | bool rv; |
490 | 524 | ||
491 | if (communication & Stdin) { | 525 | if (communication & Stdin) { |
492 | communication = (Communication) (communication & ~Stdin); | 526 | communication = (Communication) (communication & ~Stdin); |
493 | delete innot; | 527 | delete innot; |
494 | innot = 0; | 528 | innot = 0; |
495 | close(in[1]); | 529 | close(in[1]); |
496 | rv = true; | 530 | rv = true; |
497 | } else | 531 | } else |
498 | rv = false; | 532 | rv = false; |
499 | return rv; | 533 | return rv; |
500 | } | 534 | } |
501 | 535 | ||
502 | bool OProcess::closeStdout() | 536 | bool OProcess::closeStdout() |
503 | { | 537 | { |
504 | bool rv; | 538 | bool rv; |
505 | 539 | ||
506 | if (communication & Stdout) { | 540 | if (communication & Stdout) { |
@@ -859,64 +893,34 @@ QCString OProcess::searchShell() | |||
859 | 893 | ||
860 | bool OProcess::isExecutable(const QCString &filename) | 894 | bool OProcess::isExecutable(const QCString &filename) |
861 | { | 895 | { |
862 | struct stat fileinfo; | 896 | struct stat fileinfo; |
863 | 897 | ||
864 | if (filename.isEmpty()) return false; | 898 | if (filename.isEmpty()) return false; |
865 | 899 | ||
866 | // CC: we've got a valid filename, now let's see whether we can execute that file | 900 | // CC: we've got a valid filename, now let's see whether we can execute that file |
867 | 901 | ||
868 | if (-1 == stat(filename.data(), &fileinfo)) return false; | 902 | if (-1 == stat(filename.data(), &fileinfo)) return false; |
869 | // CC: return false if the file does not exist | 903 | // CC: return false if the file does not exist |
870 | 904 | ||
871 | // CC: anyway, we cannot execute directories, block/character devices, fifos or sockets | 905 | // CC: anyway, we cannot execute directories, block/character devices, fifos or sockets |
872 | if ( (S_ISDIR(fileinfo.st_mode)) || | 906 | if ( (S_ISDIR(fileinfo.st_mode)) || |
873 | (S_ISCHR(fileinfo.st_mode)) || | 907 | (S_ISCHR(fileinfo.st_mode)) || |
874 | (S_ISBLK(fileinfo.st_mode)) || | 908 | (S_ISBLK(fileinfo.st_mode)) || |
875 | #ifdef S_ISSOCK | 909 | #ifdef S_ISSOCK |
876 | // CC: SYSVR4 systems don't have that macro | 910 | // CC: SYSVR4 systems don't have that macro |
877 | (S_ISSOCK(fileinfo.st_mode)) || | 911 | (S_ISSOCK(fileinfo.st_mode)) || |
878 | #endif | 912 | #endif |
879 | (S_ISFIFO(fileinfo.st_mode)) || | 913 | (S_ISFIFO(fileinfo.st_mode)) || |
880 | (S_ISDIR(fileinfo.st_mode)) ) { | 914 | (S_ISDIR(fileinfo.st_mode)) ) { |
881 | return false; | 915 | return false; |
882 | } | 916 | } |
883 | 917 | ||
884 | // CC: now check for permission to execute the file | 918 | // CC: now check for permission to execute the file |
885 | if (access(filename.data(), X_OK) != 0) return false; | 919 | if (access(filename.data(), X_OK) != 0) return false; |
886 | 920 | ||
887 | // CC: we've passed all the tests... | 921 | // CC: we've passed all the tests... |
888 | return true; | 922 | return true; |
889 | } | 923 | } |
890 | 924 | ||
891 | void OProcess::virtual_hook( int, void* ) | ||
892 | { /*BASE::virtual_hook( id, data );*/ } | ||
893 | |||
894 | |||
895 | /////////////////////////// | ||
896 | // CC: Class KShellProcess | ||
897 | /////////////////////////// | ||
898 | |||
899 | KShellProcess::KShellProcess(const char *shellname): | ||
900 | OProcess() | ||
901 | { | ||
902 | setUseShell(true, shellname); | ||
903 | } | ||
904 | |||
905 | |||
906 | KShellProcess::~KShellProcess() { | ||
907 | } | ||
908 | |||
909 | QString KShellProcess::quote(const QString &arg) | ||
910 | { | ||
911 | return OProcess::quote(arg); | ||
912 | } | ||
913 | |||
914 | bool KShellProcess::start(RunMode runmode, Communication comm) | ||
915 | { | ||
916 | return OProcess::start(runmode, comm); | ||
917 | } | ||
918 | 925 | ||
919 | void KShellProcess::virtual_hook( int id, void* data ) | ||
920 | { OProcess::virtual_hook( id, data ); } | ||
921 | 926 | ||
922 | //#include "kprocess.moc" | ||