├── LICENSE ├── Makefile ├── README.md ├── cppngrok.cpp ├── cppngrok.h └── pstream.h /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PWD := `pwd` 2 | all: cppngrok.cpp 3 | @ mkdir -p build 4 | @ g++ -c cppngrok.cpp -o ./build/cppngrok.o 5 | @ ar crv cppngrok_lib.a ./build/cppngrok.o --output build 6 | @ mv cppngrok_lib.a ./build 7 | shared: 8 | @ g++ -c -fpic -o ./build/cppngrok_so.o cppngrok.cpp 9 | @ g++ -shared -o ./build/cppngrok_slib.so ./build/cppngrok_so.o 10 | clean: 11 | @ shred -u ./build/cppngrok.o ./build/cppngrok_lib.a 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cppngrok 2 | a cpp wrapper for ngrok 3 | 4 |
5 | 6 | ### basic usage 7 | 8 | ```cpp 9 | 10 | // warning - you are in testing branch, so some things arent complete stable or implemented 11 | 12 | #include 13 | #include "cppngrok.h" // or if instaled cppngrok.h in includes 14 | 15 | int main(){ 16 | CppngrokHandler handler(true); // "true" is a argument for constructor to enable logger, use false to deactivate 17 | struct address_handler *recv_addr; 18 | 19 | recv_addr = handler.bind(methods::http, 1337); // binding http for port 1337 20 | std::cout << "HTTP ADDRESS :: " << recv_addr->ext_http << endl; 21 | 22 | recv_addr = handler.bind(methods::tcp, 1338); // binding tcp for port 1338 23 | std::cout << "TCP ADDRESS :: " << recv_addr->ext_tcp << endl; 24 | std::cout << "TCP PORT :: " << recv_addr->ext_tcp_port << endl; 25 | 26 | recv_addr = handler.bind(method::tcp) // binding tcp for default port (1337) 27 | std::cout << "TCP ADDRESS :: " << recv_addr->ext_tcp << endl; 28 | std::cout << "TCP PORT :: " << recv_addr->ext_tcp_port << endl; 29 | 30 | recv_addr = handler.bind() // binding http for port 80 31 | std::cout << "HTTP ADDRESS :: " << recv_addr->ext_http << endl; 32 | } 33 | ``` 34 | 35 | ### requeriments 36 | ``` 37 | https://ngrok.com/ 38 | ``` 39 | 40 | ### simple static build 41 | 42 | ``` 43 | make 44 | g++ your_project.cpp build/cppngrok_lib.a 45 | make clean 46 | ``` 47 | 48 | -------------------------------------------------------------------------------- /cppngrok.cpp: -------------------------------------------------------------------------------- 1 | #include "cppngrok.h" 2 | 3 | /* 4 | * AUTHOR : demon-i386 / KB1te 5 | * LICENSE : GPL v3.0 6 | * VERSION : 0.1.0 7 | */ 8 | 9 | using namespace std; 10 | bool log_handler; 11 | const char* path; 12 | address_handler *addr_h; 13 | // 14 | 15 | 16 | 17 | address_handler *CppngrokHandler::UrlBuilder(std::string regex_food) { // address_handler alocator, this will alocate address_handler structure with data and return address_handler 18 | std::regex http_rgx("http://[a-zA-Z0-9.ngrok.io]+[^a-zA-Z/:localhost]io"); 19 | std::regex tcp_rgx("tcp://[a-zA-Z0-9.]+[^a-zA-Z/:localhost]io"); 20 | std::regex tls_rgx("tls://[a-zA-Z0-9.]+[^a-zA-Z/:localhost]io"); 21 | std::smatch match; 22 | if (std::regex_search(regex_food, match, http_rgx)) { 23 | cout << "HTTP ADDRESS FOUND :: " << match[0] << endl; 24 | addr_h->ext_http = match[0]; 25 | } 26 | if (std::regex_search(regex_food, match, tcp_rgx)) { 27 | cout << "TCP ADDRESS FOUND :: " << match[0] << endl; 28 | addr_h->ext_tcp = match[0]; 29 | } 30 | if (std::regex_match(regex_food, match, tls_rgx)) { 31 | addr_h->ext_tls = match[0]; 32 | } 33 | return addr_h; 34 | } 35 | 36 | address_handler *CppngrokHandler::bind() { // return type = struct address_handler declared in cppngrok.h 37 | char *buff; 38 | buff = (char *) path; 39 | redi::ipstream is((string)buff+(string)" http 80 --log stdout"); 40 | std::string outBuff; 41 | while(std::getline(is, outBuff)){ 42 | addr_h = CppngrokHandler::UrlBuilder(outBuff); 43 | if(!addr_h->ext_http.empty()){ 44 | is.close(); 45 | } 46 | } 47 | return addr_h; 48 | } 49 | 50 | 51 | address_handler *CppngrokHandler::bind(methods ngrokmethod, unsigned int port) { 52 | char *buff; 53 | buff = (char *) path; 54 | if(sizeof(port) > 5){ 55 | cout << "FUCK U"; 56 | exit(1); 57 | } 58 | char command[25]; 59 | char char_port[5 + sizeof(char)]; 60 | char method[4 + sizeof(char)]; 61 | 62 | std::sprintf(method, "%s", methods_str[ngrokmethod]); 63 | std::sprintf(char_port, "%d", port); 64 | std::sprintf(command, " %s %s --log stdout", methods_str[ngrokmethod], char_port); 65 | 66 | redi::ipstream is ((string) buff + (string)command); 67 | 68 | memset(command, 0, sizeof(command)); 69 | memset(char_port, 0, sizeof(char_port)); 70 | memset(method, 0, sizeof(method)); 71 | 72 | std::string outBuff; 73 | 74 | switch (ngrokmethod) { 75 | case methods::http: 76 | while(std::getline(is, outBuff)){ 77 | addr_h = CppngrokHandler::UrlBuilder(outBuff); 78 | if(!addr_h->ext_http.empty()){ 79 | cout << "RETURNED HTTP :: " << addr_h->ext_http << endl; 80 | is.close(); 81 | if (is.rdbuf()->exited()){ 82 | cout << "EXITED" << endl; 83 | 84 | } 85 | } 86 | } 87 | break; 88 | case methods::tcp: 89 | while(std::getline(is, outBuff)){ 90 | addr_h = CppngrokHandler::UrlBuilder(outBuff); 91 | if(!addr_h->ext_tcp.empty()){ 92 | cout << "RETURNED TCP :: " << addr_h->ext_tcp << endl; 93 | is.close(); 94 | } 95 | } 96 | break; 97 | case methods::tls: 98 | while(std::getline(is, outBuff)){ 99 | addr_h = CppngrokHandler::UrlBuilder(outBuff); 100 | if(!addr_h->ext_tls.empty()){ 101 | is.close(); 102 | } 103 | } 104 | } 105 | return addr_h; 106 | } 107 | 108 | 109 | int CppngrokHandler::logger(std::string message, int log_level, bool log_handler) { 110 | printf("[%d]", log_handler); 111 | if (log_handler == false) { 112 | return 0; 113 | } 114 | switch (log_level) { 115 | case 1: 116 | cout << "[NOTE] " << message << endl; 117 | break; 118 | case 2: 119 | cout << "[WARNING] " << message << endl; 120 | break; 121 | case 3: 122 | cout << "[ERROR] " << message << endl; 123 | break; 124 | case 4: 125 | cout << "[CRITICAL] " << message << endl; 126 | break; 127 | case 5: 128 | cout << "[DEBUG] " << message << endl; 129 | break; 130 | case 0: 131 | break; 132 | } 133 | return 0; 134 | } 135 | 136 | 137 | CppngrokHandler::CppngrokHandler(bool log_opt) { 138 | addr_h = (address_handler*)calloc(sizeof(std::string) * 3, sizeof(addr_h)); 139 | log_handler = log_opt; 140 | char *buff = { std::getenv("HOME") }; 141 | strncat(buff, "/.local/bin/ngrok", 17); 142 | path = buff; 143 | if (FILE *file = fopen(buff, "rb")) { 144 | fclose(file); 145 | } 146 | else { 147 | if (std::getenv("NGROK_PATH")) { 148 | buff = std::getenv("NGROK_PATH"); 149 | } 150 | else { 151 | CppngrokHandler::logger("PLEASE SET NGROK PATH TO ENV 'NGROK_PATH' AND TRY AGAIN", 3, log_handler); 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /cppngrok.h: -------------------------------------------------------------------------------- 1 | // AUTHOR : demon-i38 / KB1te 2 | // LICENSE : GPL 3.0 3 | // VERSION : 0.1.0 4 | 5 | #pragma once 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "pstream.h" 14 | 15 | using namespace std; 16 | 17 | enum methods { 18 | tcp, 19 | tls, 20 | http 21 | }; 22 | 23 | 24 | static const char *methods_str[] = { "tcp", "tls", "http" }; 25 | 26 | struct address_handler{ 27 | string ext_http; 28 | string ext_tcp; 29 | string ext_tls; 30 | unsigned int ext_tcp_port; 31 | unsigned int ext_tls_port; 32 | }; 33 | 34 | class CppngrokHandler{ 35 | public: 36 | CppngrokHandler(bool log_opt); 37 | int logger(std::string message, int log_level, bool log_handler); 38 | address_handler* bind(); 39 | address_handler* UrlBuilder(std::string regexfood = "NONE"); 40 | address_handler* bind(methods ngrokmethods, unsigned int port = 1337); 41 | }; 42 | 43 | -------------------------------------------------------------------------------- /pstream.h: -------------------------------------------------------------------------------- 1 | // PStreams - POSIX Process I/O for C++ 2 | 3 | // Copyright (C) 2001 - 2020 Jonathan Wakely 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See accompanying file LICENSE_1_0.txt or copy at 6 | // http://www.boost.org/LICENSE_1_0.txt) 7 | // 8 | // SPDX-License-Identifier: BSL-1.0 9 | 10 | /** 11 | * @file pstream.h 12 | * @brief Declares all PStreams classes. 13 | * @author Jonathan Wakely 14 | * 15 | * Defines classes redi::ipstream, redi::opstream, redi::pstream 16 | * and redi::rpstream. 17 | */ 18 | 19 | #ifndef REDI_PSTREAM_H_SEEN 20 | #define REDI_PSTREAM_H_SEEN 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include // for min() 29 | #include // for errno 30 | #include // for size_t, NULL 31 | #include // for exit() 32 | #include // for pid_t 33 | #include // for waitpid() 34 | #include // for ioctl() and FIONREAD 35 | #if defined(__sun) 36 | # include // for FIONREAD on Solaris 2.5 37 | #endif 38 | #include // for pipe() fork() exec() and filedes functions 39 | #include // for kill() 40 | #include // for fcntl() 41 | #if REDI_EVISCERATE_PSTREAMS 42 | # include // for FILE, fdopen() 43 | #endif 44 | 45 | 46 | /// The library version. 47 | #define PSTREAMS_VERSION 0x0103 // 1.0.3 48 | 49 | /** 50 | * @namespace redi 51 | * @brief All PStreams classes are declared in namespace redi. 52 | * 53 | * Like the standard iostreams, PStreams is a set of class templates, 54 | * taking a character type and traits type. As with the standard streams 55 | * they are most likely to be used with @c char and the default 56 | * traits type, so typedefs for this most common case are provided. 57 | * 58 | * The @c pstream_common class template is not intended to be used directly, 59 | * it is used internally to provide the common functionality for the 60 | * other stream classes. 61 | */ 62 | namespace redi 63 | { 64 | /// Common base class providing constants and typenames. 65 | struct pstreams 66 | { 67 | /// Type used to specify how to connect to the process. 68 | typedef std::ios_base::openmode pmode; 69 | 70 | /// Type used to hold the arguments for a command. 71 | typedef std::vector argv_type; 72 | 73 | /// Type used for file descriptors. 74 | typedef int fd_type; 75 | 76 | static const pmode pstdin = std::ios_base::out; ///< Write to stdin 77 | static const pmode pstdout = std::ios_base::in; ///< Read from stdout 78 | static const pmode pstderr = std::ios_base::app; ///< Read from stderr 79 | 80 | /// Create a new process group for the child process. 81 | static const pmode newpg = std::ios_base::trunc; 82 | 83 | protected: 84 | enum { bufsz = 32 }; ///< Size of pstreambuf buffers. 85 | enum { pbsz = 2 }; ///< Number of putback characters kept. 86 | 87 | #if __cplusplus >= 201103L 88 | template 89 | using stringable = decltype((void)std::string(std::declval())); 90 | #endif 91 | }; 92 | 93 | /// Class template for stream buffer. 94 | template > 95 | class basic_pstreambuf 96 | : public std::basic_streambuf 97 | , public pstreams 98 | { 99 | public: 100 | // Type definitions for dependent types 101 | typedef CharT char_type; 102 | typedef Traits traits_type; 103 | typedef typename traits_type::int_type int_type; 104 | typedef typename traits_type::off_type off_type; 105 | typedef typename traits_type::pos_type pos_type; 106 | /** @deprecated use pstreams::fd_type instead. */ 107 | typedef fd_type fd_t; 108 | 109 | /// Default constructor. 110 | basic_pstreambuf(); 111 | 112 | /// Constructor that initialises the buffer with @a cmd. 113 | basic_pstreambuf(const std::string& cmd, pmode mode); 114 | 115 | /// Constructor that initialises the buffer with @a file and @a argv. 116 | basic_pstreambuf( const std::string& file, 117 | const argv_type& argv, 118 | pmode mode ); 119 | 120 | #if __cplusplus >= 201103L 121 | basic_pstreambuf(basic_pstreambuf&&) noexcept; 122 | basic_pstreambuf& operator=(basic_pstreambuf&&) noexcept; 123 | void swap(basic_pstreambuf&) noexcept; 124 | #endif 125 | 126 | /// Destructor. 127 | ~basic_pstreambuf(); 128 | 129 | /// Initialise the stream buffer with @a cmd. 130 | basic_pstreambuf* 131 | open(const std::string& cmd, pmode mode); 132 | 133 | /// Initialise the stream buffer with @a file and @a argv. 134 | basic_pstreambuf* 135 | open(const std::string& file, const argv_type& argv, pmode mode); 136 | 137 | /// Close the stream buffer and wait for the process to exit. 138 | basic_pstreambuf* 139 | close(); 140 | 141 | /// Send a signal to the process. 142 | basic_pstreambuf* 143 | kill(int signal = SIGTERM); 144 | 145 | /// Send a signal to the process' process group. 146 | basic_pstreambuf* 147 | killpg(int signal = SIGTERM); 148 | 149 | /// Close the pipe connected to the process' stdin. 150 | void 151 | peof(); 152 | 153 | /// Change active input source. 154 | bool 155 | read_err(bool readerr = true); 156 | 157 | /// Report whether the stream buffer has been initialised. 158 | bool 159 | is_open() const; 160 | 161 | /// Report whether the process has exited. 162 | bool 163 | exited(); 164 | 165 | #if REDI_EVISCERATE_PSTREAMS 166 | /// Obtain FILE pointers for each of the process' standard streams. 167 | std::size_t 168 | fopen(FILE*& in, FILE*& out, FILE*& err); 169 | #endif 170 | 171 | /// Return the exit status of the process. 172 | int 173 | status() const; 174 | 175 | /// Return the error number (errno) for the most recent failed operation. 176 | int 177 | error() const; 178 | 179 | protected: 180 | /// Transfer characters to the pipe when character buffer overflows. 181 | int_type 182 | overflow(int_type c); 183 | 184 | /// Transfer characters from the pipe when the character buffer is empty. 185 | int_type 186 | underflow(); 187 | 188 | /// Make a character available to be returned by the next extraction. 189 | int_type 190 | pbackfail(int_type c = traits_type::eof()); 191 | 192 | /// Write any buffered characters to the stream. 193 | int 194 | sync(); 195 | 196 | /// Insert multiple characters into the pipe. 197 | std::streamsize 198 | xsputn(const char_type* s, std::streamsize n); 199 | 200 | /// Insert a sequence of characters into the pipe. 201 | std::streamsize 202 | write(const char_type* s, std::streamsize n); 203 | 204 | /// Extract a sequence of characters from the pipe. 205 | std::streamsize 206 | read(char_type* s, std::streamsize n); 207 | 208 | /// Report how many characters can be read from active input without blocking. 209 | std::streamsize 210 | showmanyc(); 211 | 212 | protected: 213 | /// Enumerated type to indicate whether stdout or stderr is to be read. 214 | enum buf_read_src { rsrc_out = 0, rsrc_err = 1 }; 215 | 216 | /// Initialise pipes and fork process. 217 | pid_t 218 | fork(pmode mode); 219 | 220 | /// Wait for the child process to exit. 221 | int 222 | wait(bool nohang = false); 223 | 224 | /// Return the file descriptor for the output pipe. 225 | fd_type& 226 | wpipe(); 227 | 228 | /// Return the file descriptor for the active input pipe. 229 | fd_type& 230 | rpipe(); 231 | 232 | /// Return the file descriptor for the specified input pipe. 233 | fd_type& 234 | rpipe(buf_read_src which); 235 | 236 | void 237 | create_buffers(pmode mode); 238 | 239 | void 240 | destroy_buffers(pmode mode); 241 | 242 | /// Writes buffered characters to the process' stdin pipe. 243 | bool 244 | empty_buffer(); 245 | 246 | bool 247 | fill_buffer(bool non_blocking = false); 248 | 249 | /// Return the active input buffer. 250 | char_type* 251 | rbuffer(); 252 | 253 | buf_read_src 254 | switch_read_buffer(buf_read_src); 255 | 256 | private: 257 | #if __cplusplus >= 201103L 258 | using basic_streambuf = std::basic_streambuf; 259 | #else 260 | basic_pstreambuf(const basic_pstreambuf&); 261 | basic_pstreambuf& operator=(const basic_pstreambuf&); 262 | #endif 263 | 264 | void 265 | init_rbuffers(); 266 | 267 | pid_t ppid_; // pid of process 268 | fd_type wpipe_; // pipe used to write to process' stdin 269 | fd_type rpipe_[2]; // two pipes to read from, stdout and stderr 270 | char_type* wbuffer_; 271 | char_type* rbuffer_[2]; 272 | char_type* rbufstate_[3]; 273 | /// Index into rpipe_[] to indicate active source for read operations. 274 | buf_read_src rsrc_; 275 | int status_; // hold exit status of child process 276 | int error_; // hold errno if fork() or exec() fails 277 | }; 278 | 279 | /// Class template for common base class. 280 | template > 281 | class pstream_common 282 | : virtual public std::basic_ios 283 | , virtual public pstreams 284 | { 285 | protected: 286 | typedef basic_pstreambuf streambuf_type; 287 | typedef std::basic_ios ios_type; 288 | 289 | typedef pstreams::pmode pmode; 290 | typedef pstreams::argv_type argv_type; 291 | 292 | /// Default constructor. 293 | pstream_common(); 294 | 295 | /// Constructor that initialises the stream by starting a process. 296 | pstream_common(const std::string& cmd, pmode mode); 297 | 298 | /// Constructor that initialises the stream by starting a process. 299 | pstream_common(const std::string& file, const argv_type& argv, pmode mode); 300 | 301 | /// Pure virtual destructor. 302 | virtual 303 | ~pstream_common() = 0; 304 | 305 | #if __cplusplus >= 201103L 306 | pstream_common(pstream_common&& rhs) noexcept 307 | : command_(std::move(rhs.command_)) 308 | , buf_(std::move(rhs.buf_)) 309 | { 310 | /* derived class is responsible for ios_type::move(rhs) happening */ 311 | } 312 | 313 | pstream_common& 314 | operator=(pstream_common&& rhs) noexcept 315 | { 316 | command_ = std::move(rhs.command_); 317 | buf_ = std::move(rhs.buf_); 318 | return *this; 319 | } 320 | 321 | void 322 | swap(pstream_common& rhs) noexcept 323 | { 324 | /* derived class is responsible for ios_type::swap(rhs) happening */ 325 | command_.swap(rhs.command_); 326 | buf_.swap(rhs.buf_); 327 | } 328 | #endif // C++11 329 | 330 | /// Start a process. 331 | void 332 | do_open(const std::string& cmd, pmode mode); 333 | 334 | /// Start a process. 335 | void 336 | do_open(const std::string& file, const argv_type& argv, pmode mode); 337 | 338 | public: 339 | /// Close the pipe, returning the program's exit status, as 340 | /// pclose(3) does. 341 | int 342 | close(); 343 | 344 | /// Report whether the stream's buffer has been initialised. 345 | bool 346 | is_open() const; 347 | 348 | /// Return the command used to initialise the stream. 349 | const std::string& 350 | command() const; 351 | 352 | /// Return a pointer to the stream buffer. 353 | streambuf_type* 354 | rdbuf() const; 355 | 356 | #if REDI_EVISCERATE_PSTREAMS 357 | /// Obtain FILE pointers for each of the process' standard streams. 358 | std::size_t 359 | fopen(FILE*& in, FILE*& out, FILE*& err); 360 | #endif 361 | 362 | protected: 363 | std::string command_; ///< The command used to start the process. 364 | streambuf_type buf_; ///< The stream buffer. 365 | }; 366 | 367 | 368 | /** 369 | * @class basic_ipstream 370 | * @brief Class template for Input PStreams. 371 | * 372 | * Reading from an ipstream reads the command's standard output and/or 373 | * standard error (depending on how the ipstream is opened) 374 | * and the command's standard input is the same as that of the process 375 | * that created the object, unless altered by the command itself. 376 | */ 377 | 378 | template > 379 | class basic_ipstream 380 | : public std::basic_istream 381 | , public pstream_common 382 | , virtual public pstreams 383 | { 384 | typedef std::basic_istream istream_type; 385 | typedef pstream_common pbase_type; 386 | 387 | using pbase_type::buf_; // declare name in this scope 388 | 389 | // Ensure a basic_ipstream will read from at least one pipe 390 | pmode readable(pmode mode) 391 | { 392 | if (!(mode & (pstdout|pstderr))) 393 | mode |= pstdout; 394 | return mode; 395 | } 396 | 397 | public: 398 | /// Type used to specify how to connect to the process. 399 | typedef typename pbase_type::pmode pmode; 400 | 401 | /// Type used to hold the arguments for a command. 402 | typedef typename pbase_type::argv_type argv_type; 403 | 404 | /// Default constructor, creates an uninitialised stream. 405 | basic_ipstream() 406 | : istream_type(NULL), pbase_type() 407 | { } 408 | 409 | /** 410 | * @brief Constructor that initialises the stream by starting a process. 411 | * 412 | * Initialises the stream buffer by calling do_open() with the supplied 413 | * arguments. 414 | * 415 | * @param cmd a string containing a shell command. 416 | * @param mode the I/O mode to use when opening the pipe. 417 | * @see do_open(const std::string&, pmode) 418 | */ 419 | explicit 420 | basic_ipstream(const std::string& cmd, pmode mode = pstdout) 421 | : istream_type(NULL), pbase_type(cmd, readable(mode)) 422 | { } 423 | 424 | /** 425 | * @brief Constructor that initialises the stream by starting a process. 426 | * 427 | * Initialises the stream buffer by calling do_open() with the supplied 428 | * arguments. 429 | * 430 | * @param file a string containing the pathname of a program to execute. 431 | * @param argv a vector of argument strings passed to the new program. 432 | * @param mode the I/O mode to use when opening the pipe. 433 | * @see do_open(const std::string&, const argv_type&, pmode) 434 | */ 435 | basic_ipstream( const std::string& file, 436 | const argv_type& argv, 437 | pmode mode = pstdout ) 438 | : istream_type(NULL), pbase_type(file, argv, readable(mode)) 439 | { } 440 | 441 | /** 442 | * @brief Constructor that initialises the stream by starting a process. 443 | * 444 | * Initialises the stream buffer by calling 445 | * @c do_open(argv[0],argv,mode|pstdout) 446 | * 447 | * @param argv a vector of argument strings passed to the new program. 448 | * @param mode the I/O mode to use when opening the pipe. 449 | * @see do_open(const std::string&, const argv_type&, pmode) 450 | */ 451 | explicit 452 | basic_ipstream(const argv_type& argv, pmode mode = pstdout) 453 | : istream_type(NULL), pbase_type(argv.at(0), argv, readable(mode)) 454 | { } 455 | 456 | #if __cplusplus >= 201103L 457 | template> 458 | explicit 459 | basic_ipstream(std::initializer_list args, pmode mode = pstdout) 460 | : basic_ipstream(argv_type(args.begin(), args.end()), mode) 461 | { } 462 | 463 | basic_ipstream(basic_ipstream&& rhs) 464 | : istream_type(std::move(rhs)) 465 | , pbase_type(std::move(rhs)) 466 | { istream_type::set_rdbuf(std::addressof(pbase_type::buf_)); } 467 | 468 | basic_ipstream& 469 | operator=(basic_ipstream&& rhs) 470 | { 471 | istream_type::operator=(std::move(rhs)); 472 | pbase_type::operator=(std::move(rhs)); 473 | return *this; 474 | } 475 | 476 | void 477 | swap(basic_ipstream& rhs) 478 | { 479 | istream_type::swap(rhs); 480 | pbase_type::swap(rhs); 481 | } 482 | #endif // C++11 483 | 484 | /** 485 | * @brief Destructor. 486 | * 487 | * Closes the stream and waits for the child to exit. 488 | */ 489 | ~basic_ipstream() 490 | { } 491 | 492 | /** 493 | * @brief Start a process. 494 | * 495 | * Calls do_open( @a cmd , @a mode|pstdout ). 496 | * 497 | * @param cmd a string containing a shell command. 498 | * @param mode the I/O mode to use when opening the pipe. 499 | * @see do_open(const std::string&, pmode) 500 | */ 501 | void 502 | open(const std::string& cmd, pmode mode = pstdout) 503 | { 504 | this->do_open(cmd, readable(mode)); 505 | } 506 | 507 | /** 508 | * @brief Start a process. 509 | * 510 | * Calls do_open( @a file , @a argv , @a mode|pstdout ). 511 | * 512 | * @param file a string containing the pathname of a program to execute. 513 | * @param argv a vector of argument strings passed to the new program. 514 | * @param mode the I/O mode to use when opening the pipe. 515 | * @see do_open(const std::string&, const argv_type&, pmode) 516 | */ 517 | void 518 | open( const std::string& file, 519 | const argv_type& argv, 520 | pmode mode = pstdout ) 521 | { 522 | this->do_open(file, argv, readable(mode)); 523 | } 524 | 525 | /** 526 | * @brief Set streambuf to read from process' @c stdout. 527 | * @return @c *this 528 | */ 529 | basic_ipstream& 530 | out() 531 | { 532 | this->buf_.read_err(false); 533 | return *this; 534 | } 535 | 536 | /** 537 | * @brief Set streambuf to read from process' @c stderr. 538 | * @return @c *this 539 | */ 540 | basic_ipstream& 541 | err() 542 | { 543 | this->buf_.read_err(true); 544 | return *this; 545 | } 546 | }; 547 | 548 | 549 | /** 550 | * @class basic_opstream 551 | * @brief Class template for Output PStreams. 552 | * 553 | * Writing to an open opstream writes to the standard input of the command; 554 | * the command's standard output is the same as that of the process that 555 | * created the pstream object, unless altered by the command itself. 556 | */ 557 | 558 | template > 559 | class basic_opstream 560 | : public std::basic_ostream 561 | , public pstream_common 562 | , virtual public pstreams 563 | { 564 | typedef std::basic_ostream ostream_type; 565 | typedef pstream_common pbase_type; 566 | 567 | using pbase_type::buf_; // declare name in this scope 568 | 569 | public: 570 | /// Type used to specify how to connect to the process. 571 | typedef typename pbase_type::pmode pmode; 572 | 573 | /// Type used to hold the arguments for a command. 574 | typedef typename pbase_type::argv_type argv_type; 575 | 576 | /// Default constructor, creates an uninitialised stream. 577 | basic_opstream() 578 | : ostream_type(NULL), pbase_type() 579 | { } 580 | 581 | /** 582 | * @brief Constructor that initialises the stream by starting a process. 583 | * 584 | * Initialises the stream buffer by calling do_open() with the supplied 585 | * arguments. 586 | * 587 | * @param cmd a string containing a shell command. 588 | * @param mode the I/O mode to use when opening the pipe. 589 | * @see do_open(const std::string&, pmode) 590 | */ 591 | explicit 592 | basic_opstream(const std::string& cmd, pmode mode = pstdin) 593 | : ostream_type(NULL), pbase_type(cmd, mode|pstdin) 594 | { } 595 | 596 | /** 597 | * @brief Constructor that initialises the stream by starting a process. 598 | * 599 | * Initialises the stream buffer by calling do_open() with the supplied 600 | * arguments. 601 | * 602 | * @param file a string containing the pathname of a program to execute. 603 | * @param argv a vector of argument strings passed to the new program. 604 | * @param mode the I/O mode to use when opening the pipe. 605 | * @see do_open(const std::string&, const argv_type&, pmode) 606 | */ 607 | basic_opstream( const std::string& file, 608 | const argv_type& argv, 609 | pmode mode = pstdin ) 610 | : ostream_type(NULL), pbase_type(file, argv, mode|pstdin) 611 | { } 612 | 613 | /** 614 | * @brief Constructor that initialises the stream by starting a process. 615 | * 616 | * Initialises the stream buffer by calling 617 | * @c do_open(argv[0],argv,mode|pstdin) 618 | * 619 | * @param argv a vector of argument strings passed to the new program. 620 | * @param mode the I/O mode to use when opening the pipe. 621 | * @see do_open(const std::string&, const argv_type&, pmode) 622 | */ 623 | explicit 624 | basic_opstream(const argv_type& argv, pmode mode = pstdin) 625 | : ostream_type(NULL), pbase_type(argv.at(0), argv, mode|pstdin) 626 | { } 627 | 628 | #if __cplusplus >= 201103L 629 | /** 630 | * @brief Constructor that initialises the stream by starting a process. 631 | * 632 | * @param args a list of argument strings passed to the new program. 633 | * @param mode the I/O mode to use when opening the pipe. 634 | * @see do_open(const std::string&, const argv_type&, pmode) 635 | */ 636 | template> 637 | explicit 638 | basic_opstream(std::initializer_list args, pmode mode = pstdin) 639 | : basic_opstream(argv_type(args.begin(), args.end()), mode) 640 | { } 641 | 642 | basic_opstream(basic_opstream&& rhs) 643 | : ostream_type(std::move(rhs)) 644 | , pbase_type(std::move(rhs)) 645 | { ostream_type::set_rdbuf(std::addressof(pbase_type::buf_)); } 646 | 647 | basic_opstream& 648 | operator=(basic_opstream&& rhs) 649 | { 650 | ostream_type::operator=(std::move(rhs)); 651 | pbase_type::operator=(std::move(rhs)); 652 | return *this; 653 | } 654 | 655 | void 656 | swap(basic_opstream& rhs) 657 | { 658 | ostream_type::swap(rhs); 659 | pbase_type::swap(rhs); 660 | } 661 | #endif // C++11 662 | 663 | /** 664 | * @brief Destructor 665 | * 666 | * Closes the stream and waits for the child to exit. 667 | */ 668 | ~basic_opstream() { } 669 | 670 | /** 671 | * @brief Start a process. 672 | * 673 | * Calls do_open( @a cmd , @a mode|pstdin ). 674 | * 675 | * @param cmd a string containing a shell command. 676 | * @param mode the I/O mode to use when opening the pipe. 677 | * @see do_open(const std::string&, pmode) 678 | */ 679 | void 680 | open(const std::string& cmd, pmode mode = pstdin) 681 | { 682 | this->do_open(cmd, mode|pstdin); 683 | } 684 | 685 | /** 686 | * @brief Start a process. 687 | * 688 | * Calls do_open( @a file , @a argv , @a mode|pstdin ). 689 | * 690 | * @param file a string containing the pathname of a program to execute. 691 | * @param argv a vector of argument strings passed to the new program. 692 | * @param mode the I/O mode to use when opening the pipe. 693 | * @see do_open(const std::string&, const argv_type&, pmode) 694 | */ 695 | void 696 | open( const std::string& file, 697 | const argv_type& argv, 698 | pmode mode = pstdin) 699 | { 700 | this->do_open(file, argv, mode|pstdin); 701 | } 702 | }; 703 | 704 | 705 | /** 706 | * @class basic_pstream 707 | * @brief Class template for Bidirectional PStreams. 708 | * 709 | * Writing to a pstream opened with @c pmode @c pstdin writes to the 710 | * standard input of the command. 711 | * Reading from a pstream opened with @c pmode @c pstdout and/or @c pstderr 712 | * reads the command's standard output and/or standard error. 713 | * Any of the process' @c stdin, @c stdout or @c stderr that is not 714 | * connected to the pstream (as specified by the @c pmode) 715 | * will be the same as the process that created the pstream object, 716 | * unless altered by the command itself. 717 | */ 718 | template > 719 | class basic_pstream 720 | : public std::basic_iostream 721 | , public pstream_common 722 | , virtual public pstreams 723 | { 724 | typedef std::basic_iostream iostream_type; 725 | typedef pstream_common pbase_type; 726 | 727 | using pbase_type::buf_; // declare name in this scope 728 | 729 | public: 730 | /// Type used to specify how to connect to the process. 731 | typedef typename pbase_type::pmode pmode; 732 | 733 | /// Type used to hold the arguments for a command. 734 | typedef typename pbase_type::argv_type argv_type; 735 | 736 | /// Default constructor, creates an uninitialised stream. 737 | basic_pstream() 738 | : iostream_type(NULL), pbase_type() 739 | { } 740 | 741 | /** 742 | * @brief Constructor that initialises the stream by starting a process. 743 | * 744 | * Initialises the stream buffer by calling do_open() with the supplied 745 | * arguments. 746 | * 747 | * @param cmd a string containing a shell command. 748 | * @param mode the I/O mode to use when opening the pipe. 749 | * @see do_open(const std::string&, pmode) 750 | */ 751 | explicit 752 | basic_pstream(const std::string& cmd, pmode mode = pstdout|pstdin) 753 | : iostream_type(NULL), pbase_type(cmd, mode) 754 | { } 755 | 756 | /** 757 | * @brief Constructor that initialises the stream by starting a process. 758 | * 759 | * Initialises the stream buffer by calling do_open() with the supplied 760 | * arguments. 761 | * 762 | * @param file a string containing the pathname of a program to execute. 763 | * @param argv a vector of argument strings passed to the new program. 764 | * @param mode the I/O mode to use when opening the pipe. 765 | * @see do_open(const std::string&, const argv_type&, pmode) 766 | */ 767 | basic_pstream( const std::string& file, 768 | const argv_type& argv, 769 | pmode mode = pstdout|pstdin ) 770 | : iostream_type(NULL), pbase_type(file, argv, mode) 771 | { } 772 | 773 | /** 774 | * @brief Constructor that initialises the stream by starting a process. 775 | * 776 | * Initialises the stream buffer by calling 777 | * @c do_open(argv[0],argv,mode) 778 | * 779 | * @param argv a vector of argument strings passed to the new program. 780 | * @param mode the I/O mode to use when opening the pipe. 781 | * @see do_open(const std::string&, const argv_type&, pmode) 782 | */ 783 | explicit 784 | basic_pstream(const argv_type& argv, pmode mode = pstdout|pstdin) 785 | : iostream_type(NULL), pbase_type(argv.at(0), argv, mode) 786 | { } 787 | 788 | #if __cplusplus >= 201103L 789 | /** 790 | * @brief Constructor that initialises the stream by starting a process. 791 | * 792 | * @param l a list of argument strings passed to the new program. 793 | * @param mode the I/O mode to use when opening the pipe. 794 | * @see do_open(const std::string&, const argv_type&, pmode) 795 | */ 796 | template> 797 | explicit 798 | basic_pstream(std::initializer_list l, pmode mode = pstdout|pstdin) 799 | : basic_pstream(argv_type(l.begin(), l.end()), mode) 800 | { } 801 | 802 | basic_pstream(basic_pstream&& rhs) 803 | : iostream_type(std::move(rhs)) 804 | , pbase_type(std::move(rhs)) 805 | { iostream_type::set_rdbuf(std::addressof(pbase_type::buf_)); } 806 | 807 | basic_pstream& 808 | operator=(basic_pstream&& rhs) 809 | { 810 | iostream_type::operator=(std::move(rhs)); 811 | pbase_type::operator=(std::move(rhs)); 812 | return *this; 813 | } 814 | 815 | void 816 | swap(basic_pstream& rhs) 817 | { 818 | iostream_type::swap(rhs); 819 | pbase_type::swap(rhs); 820 | } 821 | #endif // C++11 822 | 823 | /** 824 | * @brief Destructor 825 | * 826 | * Closes the stream and waits for the child to exit. 827 | */ 828 | ~basic_pstream() { } 829 | 830 | /** 831 | * @brief Start a process. 832 | * 833 | * Calls do_open( @a cnd , @a mode ). 834 | * 835 | * @param cmd a string containing a shell command. 836 | * @param mode the I/O mode to use when opening the pipe. 837 | * @see do_open(const std::string&, pmode) 838 | */ 839 | void 840 | open(const std::string& cmd, pmode mode = pstdout|pstdin) 841 | { 842 | this->do_open(cmd, mode); 843 | } 844 | 845 | /** 846 | * @brief Start a process. 847 | * 848 | * Calls do_open( @a file , @a argv , @a mode ). 849 | * 850 | * @param file a string containing the pathname of a program to execute. 851 | * @param argv a vector of argument strings passed to the new program. 852 | * @param mode the I/O mode to use when opening the pipe. 853 | * @see do_open(const std::string&, const argv_type&, pmode) 854 | */ 855 | void 856 | open( const std::string& file, 857 | const argv_type& argv, 858 | pmode mode = pstdout|pstdin ) 859 | { 860 | this->do_open(file, argv, mode); 861 | } 862 | 863 | /** 864 | * @brief Set streambuf to read from process' @c stdout. 865 | * @return @c *this 866 | */ 867 | basic_pstream& 868 | out() 869 | { 870 | this->buf_.read_err(false); 871 | return *this; 872 | } 873 | 874 | /** 875 | * @brief Set streambuf to read from process' @c stderr. 876 | * @return @c *this 877 | */ 878 | basic_pstream& 879 | err() 880 | { 881 | this->buf_.read_err(true); 882 | return *this; 883 | } 884 | }; 885 | 886 | 887 | /** 888 | * @class basic_rpstream 889 | * @brief Class template for Restricted PStreams. 890 | * 891 | * Writing to an rpstream opened with @c pmode @c pstdin writes to the 892 | * standard input of the command. 893 | * It is not possible to read directly from an rpstream object, to use 894 | * an rpstream as in istream you must call either basic_rpstream::out() 895 | * or basic_rpstream::err(). This is to prevent accidental reads from 896 | * the wrong input source. If the rpstream was not opened with @c pmode 897 | * @c pstderr then the class cannot read the process' @c stderr, and 898 | * basic_rpstream::err() will return an istream that reads from the 899 | * process' @c stdout, and vice versa. 900 | * Reading from an rpstream opened with @c pmode @c pstdout and/or 901 | * @c pstderr reads the command's standard output and/or standard error. 902 | * Any of the process' @c stdin, @c stdout or @c stderr that is not 903 | * connected to the pstream (as specified by the @c pmode) 904 | * will be the same as the process that created the pstream object, 905 | * unless altered by the command itself. 906 | */ 907 | 908 | template > 909 | class basic_rpstream 910 | : public std::basic_ostream 911 | , private std::basic_istream 912 | , private pstream_common 913 | , virtual public pstreams 914 | { 915 | typedef std::basic_ostream ostream_type; 916 | typedef std::basic_istream istream_type; 917 | typedef pstream_common pbase_type; 918 | 919 | using pbase_type::buf_; // declare name in this scope 920 | 921 | public: 922 | /// Type used to specify how to connect to the process. 923 | typedef typename pbase_type::pmode pmode; 924 | 925 | /// Type used to hold the arguments for a command. 926 | typedef typename pbase_type::argv_type argv_type; 927 | 928 | /// Default constructor, creates an uninitialised stream. 929 | basic_rpstream() 930 | : ostream_type(NULL), istream_type(NULL), pbase_type() 931 | { } 932 | 933 | /** 934 | * @brief Constructor that initialises the stream by starting a process. 935 | * 936 | * Initialises the stream buffer by calling do_open() with the supplied 937 | * arguments. 938 | * 939 | * @param cmd a string containing a shell command. 940 | * @param mode the I/O mode to use when opening the pipe. 941 | * @see do_open(const std::string&, pmode) 942 | */ 943 | explicit 944 | basic_rpstream(const std::string& cmd, pmode mode = pstdout|pstdin) 945 | : ostream_type(NULL) , istream_type(NULL) , pbase_type(cmd, mode) 946 | { } 947 | 948 | /** 949 | * @brief Constructor that initialises the stream by starting a process. 950 | * 951 | * Initialises the stream buffer by calling do_open() with the supplied 952 | * arguments. 953 | * 954 | * @param file a string containing the pathname of a program to execute. 955 | * @param argv a vector of argument strings passed to the new program. 956 | * @param mode the I/O mode to use when opening the pipe. 957 | * @see do_open(const std::string&, const argv_type&, pmode) 958 | */ 959 | basic_rpstream( const std::string& file, 960 | const argv_type& argv, 961 | pmode mode = pstdout|pstdin ) 962 | : ostream_type(NULL), istream_type(NULL), pbase_type(file, argv, mode) 963 | { } 964 | 965 | /** 966 | * @brief Constructor that initialises the stream by starting a process. 967 | * 968 | * Initialises the stream buffer by calling 969 | * @c do_open(argv[0],argv,mode) 970 | * 971 | * @param argv a vector of argument strings passed to the new program. 972 | * @param mode the I/O mode to use when opening the pipe. 973 | * @see do_open(const std::string&, const argv_type&, pmode) 974 | */ 975 | explicit 976 | basic_rpstream(const argv_type& argv, pmode mode = pstdout|pstdin) 977 | : ostream_type(NULL), istream_type(NULL) 978 | , pbase_type(argv.at(0), argv, mode) 979 | { } 980 | 981 | #if __cplusplus >= 201103L 982 | /** 983 | * @brief Constructor that initialises the stream by starting a process. 984 | * 985 | * @param l a list of argument strings passed to the new program. 986 | * @param mode the I/O mode to use when opening the pipe. 987 | * @see do_open(const std::string&, const argv_type&, pmode) 988 | */ 989 | template> 990 | explicit 991 | basic_rpstream(std::initializer_list l, pmode mode = pstdout|pstdin) 992 | : basic_rpstream(argv_type(l.begin(), l.end()), mode) 993 | { } 994 | 995 | // TODO: figure out how to move istream and ostream bases separately, 996 | // but so the virtual basic_ios base is only modified once. 997 | #if 0 998 | basic_rpstream(basic_rpstream&& rhs) 999 | : iostream_type(std::move(rhs)) 1000 | , pbase_type(std::move(rhs)) 1001 | { iostream_type::set_rdbuf(std::addressof(pbase_type::buf_)); } 1002 | 1003 | basic_rpstream& 1004 | operator=(basic_rpstream&& rhs) 1005 | { 1006 | iostream_type::operator=(std::move(rhs)); 1007 | pbase_type::operator=(std::move(rhs)); 1008 | return *this; 1009 | } 1010 | 1011 | void 1012 | swap(basic_rpstream& rhs) 1013 | { 1014 | iostream_type::swap(rhs); 1015 | pbase_type::swap(rhs); 1016 | } 1017 | #endif 1018 | #endif // C++11 1019 | 1020 | /// Destructor 1021 | ~basic_rpstream() { } 1022 | 1023 | /** 1024 | * @brief Start a process. 1025 | * 1026 | * Calls do_open( @a cmd , @a mode ). 1027 | * 1028 | * @param cmd a string containing a shell command. 1029 | * @param mode the I/O mode to use when opening the pipe. 1030 | * @see do_open(const std::string&, pmode) 1031 | */ 1032 | void 1033 | open(const std::string& cmd, pmode mode = pstdout|pstdin) 1034 | { 1035 | this->do_open(cmd, mode); 1036 | } 1037 | 1038 | /** 1039 | * @brief Start a process. 1040 | * 1041 | * Calls do_open( @a file , @a argv , @a mode ). 1042 | * 1043 | * @param file a string containing the pathname of a program to execute. 1044 | * @param argv a vector of argument strings passed to the new program. 1045 | * @param mode the I/O mode to use when opening the pipe. 1046 | * @see do_open(const std::string&, const argv_type&, pmode) 1047 | */ 1048 | void 1049 | open( const std::string& file, 1050 | const argv_type& argv, 1051 | pmode mode = pstdout|pstdin ) 1052 | { 1053 | this->do_open(file, argv, mode); 1054 | } 1055 | 1056 | /** 1057 | * @brief Obtain a reference to the istream that reads 1058 | * the process' @c stdout. 1059 | * @return @c *this 1060 | */ 1061 | istream_type& 1062 | out() 1063 | { 1064 | this->buf_.read_err(false); 1065 | return *this; 1066 | } 1067 | 1068 | /** 1069 | * @brief Obtain a reference to the istream that reads 1070 | * the process' @c stderr. 1071 | * @return @c *this 1072 | */ 1073 | istream_type& 1074 | err() 1075 | { 1076 | this->buf_.read_err(true); 1077 | return *this; 1078 | } 1079 | }; 1080 | 1081 | 1082 | /// Type definition for common template specialisation. 1083 | typedef basic_pstreambuf pstreambuf; 1084 | /// Type definition for common template specialisation. 1085 | typedef basic_ipstream ipstream; 1086 | /// Type definition for common template specialisation. 1087 | typedef basic_opstream opstream; 1088 | /// Type definition for common template specialisation. 1089 | typedef basic_pstream pstream; 1090 | /// Type definition for common template specialisation. 1091 | typedef basic_rpstream rpstream; 1092 | 1093 | 1094 | /** 1095 | * When inserted into an output pstream the manipulator calls 1096 | * basic_pstreambuf::peof() to close the output pipe, 1097 | * causing the child process to receive the end-of-file indicator 1098 | * on subsequent reads from its @c stdin stream. 1099 | * 1100 | * @brief Manipulator to close the pipe connected to the process' stdin. 1101 | * @param s An output PStream class. 1102 | * @return The stream object the manipulator was invoked on. 1103 | * @see basic_pstreambuf::peof() 1104 | * @relates basic_opstream basic_pstream basic_rpstream 1105 | */ 1106 | template 1107 | inline std::basic_ostream& 1108 | peof(std::basic_ostream& s) 1109 | { 1110 | typedef basic_pstreambuf pstreambuf_type; 1111 | if (pstreambuf_type* p = dynamic_cast(s.rdbuf())) 1112 | p->peof(); 1113 | return s; 1114 | } 1115 | 1116 | 1117 | /* 1118 | * member definitions for pstreambuf 1119 | */ 1120 | 1121 | 1122 | /** 1123 | * @class basic_pstreambuf 1124 | * Provides underlying streambuf functionality for the PStreams classes. 1125 | */ 1126 | 1127 | /** Creates an uninitialised stream buffer. */ 1128 | template 1129 | inline 1130 | basic_pstreambuf::basic_pstreambuf() 1131 | : ppid_(-1) // initialise to -1 to indicate no process run yet. 1132 | , wpipe_(-1) 1133 | , wbuffer_() 1134 | , rbuffer_() 1135 | , rbufstate_() 1136 | , rsrc_(rsrc_out) 1137 | , status_(-1) 1138 | , error_(0) 1139 | { 1140 | rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1; 1141 | } 1142 | 1143 | /** 1144 | * Initialises the stream buffer by calling open() with the supplied 1145 | * arguments. 1146 | * 1147 | * @param cmd a string containing a shell command. 1148 | * @param mode the I/O mode to use when opening the pipe. 1149 | * @see open() 1150 | */ 1151 | template 1152 | inline 1153 | basic_pstreambuf::basic_pstreambuf(const std::string& cmd, pmode mode) 1154 | : ppid_(-1) // initialise to -1 to indicate no process run yet. 1155 | , wpipe_(-1) 1156 | , wbuffer_() 1157 | , rbuffer_() 1158 | , rbufstate_() 1159 | , rsrc_(rsrc_out) 1160 | , status_(-1) 1161 | , error_(0) 1162 | { 1163 | rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1; 1164 | open(cmd, mode); 1165 | } 1166 | 1167 | /** 1168 | * Initialises the stream buffer by calling open() with the supplied 1169 | * arguments. 1170 | * 1171 | * @param file a string containing the name of a program to execute. 1172 | * @param argv a vector of argument strings passsed to the new program. 1173 | * @param mode the I/O mode to use when opening the pipe. 1174 | * @see open() 1175 | */ 1176 | template 1177 | inline 1178 | basic_pstreambuf::basic_pstreambuf( const std::string& file, 1179 | const argv_type& argv, 1180 | pmode mode ) 1181 | : ppid_(-1) // initialise to -1 to indicate no process run yet. 1182 | , wpipe_(-1) 1183 | , wbuffer_() 1184 | , rbuffer_() 1185 | , rbufstate_() 1186 | , rsrc_(rsrc_out) 1187 | , status_(-1) 1188 | , error_(0) 1189 | { 1190 | rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1; 1191 | open(file, argv, mode); 1192 | } 1193 | 1194 | /** 1195 | * Closes the stream by calling close(). 1196 | * @see close() 1197 | */ 1198 | template 1199 | inline 1200 | basic_pstreambuf::~basic_pstreambuf() 1201 | { 1202 | close(); 1203 | } 1204 | 1205 | #if __cplusplus >= 201103L 1206 | /** 1207 | * Move constructor. 1208 | */ 1209 | template 1210 | inline 1211 | basic_pstreambuf::basic_pstreambuf( basic_pstreambuf&& rhs ) noexcept 1212 | : basic_streambuf(static_cast(rhs)) 1213 | , ppid_(rhs.ppid_) 1214 | , wpipe_(rhs.wpipe_) 1215 | , rpipe_{rhs.rpipe_[0], rhs.rpipe_[1]} 1216 | , wbuffer_(rhs.wbuffer_) 1217 | , rbuffer_{rhs.rbuffer_[0], rhs.rbuffer_[1]} 1218 | , rbufstate_{rhs.rbufstate_[0], rhs.rbufstate_[1], rhs.rbufstate_[2]} 1219 | , rsrc_(rhs.rsrc_) 1220 | , status_(rhs.status_) 1221 | , error_(rhs.error_) 1222 | { 1223 | rhs.ppid_ = -1; 1224 | rhs.wpipe_ = -1; 1225 | rhs.rpipe_[0] = rhs.rpipe_[1] = -1; 1226 | rhs.wbuffer_ = nullptr; 1227 | rhs.rbuffer_[0] = rhs.rbuffer_[1] = nullptr; 1228 | rhs.rbufstate_[0] = rhs.rbufstate_[1] = rhs.rbufstate_[2] = nullptr; 1229 | rhs.rsrc_ = rsrc_out; 1230 | rhs.status_ = -1; 1231 | rhs.error_ = 0; 1232 | rhs.setg(nullptr, nullptr, nullptr); 1233 | rhs.setp(nullptr, nullptr); 1234 | } 1235 | 1236 | template 1237 | inline basic_pstreambuf& 1238 | basic_pstreambuf::operator=( basic_pstreambuf&& rhs ) noexcept 1239 | { 1240 | close(); 1241 | basic_streambuf::operator=(static_cast(rhs)); 1242 | swap(rhs); 1243 | return *this; 1244 | } 1245 | 1246 | template 1247 | inline void 1248 | basic_pstreambuf::swap( basic_pstreambuf& rhs ) noexcept 1249 | { 1250 | basic_streambuf::swap(static_cast(rhs)); 1251 | std::swap(ppid_, rhs.ppid_); 1252 | std::swap(wpipe_, rhs.wpipe_); 1253 | std::swap(rpipe_, rhs.rpipe_); 1254 | std::swap(wbuffer_, rhs.wbuffer_); 1255 | std::swap(rbuffer_, rhs.rbuffer_); 1256 | std::swap(rbufstate_, rhs.rbufstate_); 1257 | std::swap(rsrc_, rhs.rsrc_); 1258 | std::swap(status_, rhs.status_); 1259 | std::swap(error_, rhs.error_); 1260 | } 1261 | #endif // C++11 1262 | 1263 | /** 1264 | * Starts a new process by passing @a command to the shell (/bin/sh) 1265 | * and opens pipes to the process with the specified @a mode. 1266 | * 1267 | * If @a mode contains @c pstdout the initial read source will be 1268 | * the child process' stdout, otherwise if @a mode contains @c pstderr 1269 | * the initial read source will be the child's stderr. 1270 | * 1271 | * Will duplicate the actions of the shell in searching for an 1272 | * executable file if the specified file name does not contain a slash (/) 1273 | * character. 1274 | * 1275 | * @warning 1276 | * There is no way to tell whether the shell command succeeded, this 1277 | * function will always succeed unless resource limits (such as 1278 | * memory usage, or number of processes or open files) are exceeded. 1279 | * This means is_open() will return true even if @a command cannot 1280 | * be executed. 1281 | * Use pstreambuf::open(const std::string&, const argv_type&, pmode) 1282 | * if you need to know whether the command failed to execute. 1283 | * 1284 | * @param command a string containing a shell command. 1285 | * @param mode a bitwise OR of one or more of @c out, @c in, @c err. 1286 | * @return NULL if the shell could not be started or the 1287 | * pipes could not be opened, @c this otherwise. 1288 | * @see execl(3) 1289 | */ 1290 | template 1291 | basic_pstreambuf* 1292 | basic_pstreambuf::open(const std::string& command, pmode mode) 1293 | { 1294 | const char * shell_path = "/bin/sh"; 1295 | #if 0 1296 | const std::string argv[] = { "sh", "-c", command }; 1297 | return this->open(shell_path, argv_type(argv, argv+3), mode); 1298 | #else 1299 | basic_pstreambuf* ret = NULL; 1300 | 1301 | if (!is_open()) 1302 | { 1303 | switch(fork(mode)) 1304 | { 1305 | case 0 : 1306 | // this is the new process, exec command 1307 | ::execl(shell_path, "sh", "-c", command.c_str(), (char*)NULL); 1308 | 1309 | // can only reach this point if exec() failed 1310 | 1311 | // parent can get exit code from waitpid() 1312 | ::_exit(errno); 1313 | // using std::exit() would make static dtors run twice 1314 | 1315 | case -1 : 1316 | // couldn't fork, error already handled in pstreambuf::fork() 1317 | break; 1318 | 1319 | default : 1320 | // this is the parent process 1321 | // activate buffers 1322 | create_buffers(mode); 1323 | ret = this; 1324 | } 1325 | } 1326 | return ret; 1327 | #endif 1328 | } 1329 | 1330 | /** 1331 | * @brief Helper function to close a file descriptor. 1332 | * 1333 | * Inspects @a fd and calls close(3) if it has a non-negative value. 1334 | * 1335 | * @param fd a file descriptor. 1336 | * @relates basic_pstreambuf 1337 | */ 1338 | inline void 1339 | close_fd(pstreams::fd_type& fd) 1340 | { 1341 | if (fd >= 0 && ::close(fd) == 0) 1342 | fd = -1; 1343 | } 1344 | 1345 | /** 1346 | * @brief Helper function to close an array of file descriptors. 1347 | * 1348 | * Calls @c close_fd() on each member of the array. 1349 | * The length of the array is determined automatically by 1350 | * template argument deduction to avoid errors. 1351 | * 1352 | * @param fds an array of file descriptors. 1353 | * @relates basic_pstreambuf 1354 | */ 1355 | template 1356 | inline void 1357 | close_fd_array(pstreams::fd_type (&fds)[N]) 1358 | { 1359 | for (std::size_t i = 0; i < N; ++i) 1360 | close_fd(fds[i]); 1361 | } 1362 | 1363 | /** 1364 | * Starts a new process by executing @a file with the arguments in 1365 | * @a argv and opens pipes to the process with the specified @a mode. 1366 | * 1367 | * By convention @c argv[0] should be the file name of the file being 1368 | * executed. 1369 | * 1370 | * If @a mode contains @c pstdout the initial read source will be 1371 | * the child process' stdout, otherwise if @a mode contains @c pstderr 1372 | * the initial read source will be the child's stderr. 1373 | * 1374 | * Will duplicate the actions of the shell in searching for an 1375 | * executable file if the specified file name does not contain a slash (/) 1376 | * character. 1377 | * 1378 | * Iff @a file is successfully executed then is_open() will return true. 1379 | * Otherwise, pstreambuf::error() can be used to obtain the value of 1380 | * @c errno that was set by execvp(3) in the child process. 1381 | * 1382 | * The exit status of the new process will be returned by 1383 | * pstreambuf::status() after pstreambuf::exited() returns true. 1384 | * 1385 | * @param file a string containing the pathname of a program to execute. 1386 | * @param argv a vector of argument strings passed to the new program. 1387 | * @param mode a bitwise OR of one or more of @c out, @c in and @c err. 1388 | * @return NULL if a pipe could not be opened or if the program could 1389 | * not be executed, @c this otherwise. 1390 | * @see execvp(3) 1391 | */ 1392 | template 1393 | basic_pstreambuf* 1394 | basic_pstreambuf::open( const std::string& file, 1395 | const argv_type& argv, 1396 | pmode mode ) 1397 | { 1398 | basic_pstreambuf* ret = NULL; 1399 | 1400 | if (!is_open()) 1401 | { 1402 | // constants for read/write ends of pipe 1403 | enum { RD, WR }; 1404 | 1405 | // open another pipe and set close-on-exec 1406 | fd_type ck_exec[] = { -1, -1 }; 1407 | if (-1 == ::pipe(ck_exec) 1408 | || -1 == ::fcntl(ck_exec[RD], F_SETFD, FD_CLOEXEC) 1409 | || -1 == ::fcntl(ck_exec[WR], F_SETFD, FD_CLOEXEC)) 1410 | { 1411 | error_ = errno; 1412 | close_fd_array(ck_exec); 1413 | } 1414 | else 1415 | { 1416 | switch(fork(mode)) 1417 | { 1418 | case 0 : 1419 | // this is the new process, exec command 1420 | { 1421 | char** arg_v = new char*[argv.size()+1]; 1422 | for (std::size_t i = 0; i < argv.size(); ++i) 1423 | { 1424 | const std::string& src = argv[i]; 1425 | char*& dest = arg_v[i]; 1426 | dest = new char[src.size()+1]; 1427 | dest[ src.copy(dest, src.size()) ] = '\0'; 1428 | } 1429 | arg_v[argv.size()] = NULL; 1430 | 1431 | ::execvp(file.c_str(), arg_v); 1432 | 1433 | // can only reach this point if exec() failed 1434 | 1435 | // parent can get error code from ck_exec pipe 1436 | error_ = errno; 1437 | 1438 | while (::write(ck_exec[WR], &error_, sizeof(error_)) == -1 1439 | && errno == EINTR) 1440 | { } 1441 | 1442 | ::close(ck_exec[WR]); 1443 | ::close(ck_exec[RD]); 1444 | 1445 | ::_exit(error_); 1446 | // using std::exit() would make static dtors run twice 1447 | } 1448 | 1449 | case -1 : 1450 | // couldn't fork, error already handled in pstreambuf::fork() 1451 | close_fd_array(ck_exec); 1452 | break; 1453 | 1454 | default : 1455 | // this is the parent process 1456 | 1457 | // check child called exec() successfully 1458 | ::close(ck_exec[WR]); 1459 | switch (::read(ck_exec[RD], &error_, sizeof(error_))) 1460 | { 1461 | case 0: 1462 | // activate buffers 1463 | create_buffers(mode); 1464 | ret = this; 1465 | break; 1466 | case -1: 1467 | error_ = errno; 1468 | break; 1469 | default: 1470 | // error_ contains error code from child 1471 | // call wait() to clean up and set ppid_ to 0 1472 | this->wait(); 1473 | break; 1474 | } 1475 | ::close(ck_exec[RD]); 1476 | } 1477 | } 1478 | } 1479 | return ret; 1480 | } 1481 | 1482 | /** 1483 | * Creates pipes as specified by @a mode and calls @c fork() to create 1484 | * a new process. If the fork is successful the parent process stores 1485 | * the child's PID and the opened pipes and the child process replaces 1486 | * its standard streams with the opened pipes. 1487 | * 1488 | * If an error occurs the error code will be set to one of the possible 1489 | * errors for @c pipe() or @c fork(). 1490 | * See your system's documentation for these error codes. 1491 | * 1492 | * @param mode an OR of pmodes specifying which of the child's 1493 | * standard streams to connect to. 1494 | * @return On success the PID of the child is returned in the parent's 1495 | * context and zero is returned in the child's context. 1496 | * On error -1 is returned and the error code is set appropriately. 1497 | */ 1498 | template 1499 | pid_t 1500 | basic_pstreambuf::fork(pmode mode) 1501 | { 1502 | pid_t pid = -1; 1503 | 1504 | // Three pairs of file descriptors, for pipes connected to the 1505 | // process' stdin, stdout and stderr 1506 | // (stored in a single array so close_fd_array() can close all at once) 1507 | fd_type fd[] = { -1, -1, -1, -1, -1, -1 }; 1508 | fd_type* const pin = fd; 1509 | fd_type* const pout = fd+2; 1510 | fd_type* const perr = fd+4; 1511 | 1512 | // constants for read/write ends of pipe 1513 | enum { RD, WR }; 1514 | 1515 | // N.B. 1516 | // For the pstreambuf pin is an output stream and 1517 | // pout and perr are input streams. 1518 | 1519 | if (!error_ && mode&pstdin && ::pipe(pin)) 1520 | error_ = errno; 1521 | 1522 | if (!error_ && mode&pstdout && ::pipe(pout)) 1523 | error_ = errno; 1524 | 1525 | if (!error_ && mode&pstderr && ::pipe(perr)) 1526 | error_ = errno; 1527 | 1528 | if (!error_) 1529 | { 1530 | pid = ::fork(); 1531 | switch (pid) 1532 | { 1533 | case 0 : 1534 | { 1535 | // this is the new process 1536 | 1537 | // for each open pipe close one end and redirect the 1538 | // respective standard stream to the other end 1539 | 1540 | if (*pin >= 0) 1541 | { 1542 | ::close(pin[WR]); 1543 | ::dup2(pin[RD], STDIN_FILENO); 1544 | ::close(pin[RD]); 1545 | } 1546 | if (*pout >= 0) 1547 | { 1548 | ::close(pout[RD]); 1549 | ::dup2(pout[WR], STDOUT_FILENO); 1550 | ::close(pout[WR]); 1551 | } 1552 | if (*perr >= 0) 1553 | { 1554 | ::close(perr[RD]); 1555 | ::dup2(perr[WR], STDERR_FILENO); 1556 | ::close(perr[WR]); 1557 | } 1558 | 1559 | #ifdef _POSIX_JOB_CONTROL 1560 | if (mode&newpg) 1561 | ::setpgid(0, 0); // Change to a new process group 1562 | #endif 1563 | 1564 | break; 1565 | } 1566 | case -1 : 1567 | { 1568 | // couldn't fork for some reason 1569 | error_ = errno; 1570 | // close any open pipes 1571 | close_fd_array(fd); 1572 | break; 1573 | } 1574 | default : 1575 | { 1576 | // this is the parent process, store process' pid 1577 | ppid_ = pid; 1578 | 1579 | // store one end of open pipes and close other end 1580 | if (*pin >= 0) 1581 | { 1582 | wpipe_ = pin[WR]; 1583 | ::close(pin[RD]); 1584 | } 1585 | if (*pout >= 0) 1586 | { 1587 | rpipe_[rsrc_out] = pout[RD]; 1588 | ::close(pout[WR]); 1589 | } 1590 | if (*perr >= 0) 1591 | { 1592 | rpipe_[rsrc_err] = perr[RD]; 1593 | ::close(perr[WR]); 1594 | } 1595 | } 1596 | } 1597 | } 1598 | else 1599 | { 1600 | // close any pipes we opened before failure 1601 | close_fd_array(fd); 1602 | } 1603 | return pid; 1604 | } 1605 | 1606 | /** 1607 | * Closes all pipes and calls wait() to wait for the process to finish. 1608 | * If an error occurs the error code will be set to one of the possible 1609 | * errors for @c waitpid(). 1610 | * See your system's documentation for these errors. 1611 | * 1612 | * @return @c this on successful close or @c NULL if there is no 1613 | * process to close or if an error occurs. 1614 | */ 1615 | template 1616 | basic_pstreambuf* 1617 | basic_pstreambuf::close() 1618 | { 1619 | const bool running = is_open(); 1620 | 1621 | sync(); // this might call wait() and reap the child process 1622 | 1623 | // rather than trying to work out whether or not we need to clean up 1624 | // just do it anyway, all cleanup functions are safe to call twice. 1625 | 1626 | destroy_buffers(pstdin|pstdout|pstderr); 1627 | 1628 | // close pipes before wait() so child gets EOF/SIGPIPE 1629 | close_fd(wpipe_); 1630 | close_fd_array(rpipe_); 1631 | 1632 | do 1633 | { 1634 | error_ = 0; 1635 | } while (wait() == -1 && error() == EINTR); 1636 | 1637 | return running ? this : NULL; 1638 | } 1639 | 1640 | /** 1641 | * Used to be called on construction to initialise the arrays for reading. 1642 | * No longer used. 1643 | */ 1644 | template 1645 | #if __cplusplus >= 201402L && __has_cpp_attribute(deprecated) 1646 | [[deprecated]] 1647 | #elif __GNUC__ 1648 | __attribute__((deprecated)) 1649 | #endif 1650 | inline void 1651 | basic_pstreambuf::init_rbuffers() 1652 | { 1653 | rpipe_[rsrc_out] = rpipe_[rsrc_err] = -1; 1654 | rbuffer_[rsrc_out] = rbuffer_[rsrc_err] = NULL; 1655 | rbufstate_[0] = rbufstate_[1] = rbufstate_[2] = NULL; 1656 | } 1657 | 1658 | template 1659 | void 1660 | basic_pstreambuf::create_buffers(pmode mode) 1661 | { 1662 | if (mode & pstdin) 1663 | { 1664 | delete[] wbuffer_; 1665 | wbuffer_ = new char_type[bufsz]; 1666 | this->setp(wbuffer_, wbuffer_ + bufsz); 1667 | } 1668 | if (mode & pstdout) 1669 | { 1670 | delete[] rbuffer_[rsrc_out]; 1671 | rbuffer_[rsrc_out] = new char_type[bufsz]; 1672 | rsrc_ = rsrc_out; 1673 | this->setg(rbuffer_[rsrc_out] + pbsz, rbuffer_[rsrc_out] + pbsz, 1674 | rbuffer_[rsrc_out] + pbsz); 1675 | } 1676 | if (mode & pstderr) 1677 | { 1678 | delete[] rbuffer_[rsrc_err]; 1679 | rbuffer_[rsrc_err] = new char_type[bufsz]; 1680 | if (!(mode & pstdout)) 1681 | { 1682 | rsrc_ = rsrc_err; 1683 | this->setg(rbuffer_[rsrc_err] + pbsz, rbuffer_[rsrc_err] + pbsz, 1684 | rbuffer_[rsrc_err] + pbsz); 1685 | } 1686 | } 1687 | } 1688 | 1689 | template 1690 | void 1691 | basic_pstreambuf::destroy_buffers(pmode mode) 1692 | { 1693 | if (mode & pstdin) 1694 | { 1695 | this->setp(NULL, NULL); 1696 | delete[] wbuffer_; 1697 | wbuffer_ = NULL; 1698 | } 1699 | if (mode & pstdout) 1700 | { 1701 | if (rsrc_ == rsrc_out) 1702 | this->setg(NULL, NULL, NULL); 1703 | delete[] rbuffer_[rsrc_out]; 1704 | rbuffer_[rsrc_out] = NULL; 1705 | } 1706 | if (mode & pstderr) 1707 | { 1708 | if (rsrc_ == rsrc_err) 1709 | this->setg(NULL, NULL, NULL); 1710 | delete[] rbuffer_[rsrc_err]; 1711 | rbuffer_[rsrc_err] = NULL; 1712 | } 1713 | } 1714 | 1715 | template 1716 | typename basic_pstreambuf::buf_read_src 1717 | basic_pstreambuf::switch_read_buffer(buf_read_src src) 1718 | { 1719 | if (rsrc_ != src) 1720 | { 1721 | char_type* tmpbufstate[] = {this->eback(), this->gptr(), this->egptr()}; 1722 | this->setg(rbufstate_[0], rbufstate_[1], rbufstate_[2]); 1723 | for (std::size_t i = 0; i < 3; ++i) 1724 | rbufstate_[i] = tmpbufstate[i]; 1725 | rsrc_ = src; 1726 | } 1727 | return rsrc_; 1728 | } 1729 | 1730 | /** 1731 | * Suspends execution and waits for the associated process to exit, or 1732 | * until a signal is delivered whose action is to terminate the current 1733 | * process or to call a signal handling function. If the process has 1734 | * already exited (i.e. it is a "zombie" process) then wait() returns 1735 | * immediately. Waiting for the child process causes all its system 1736 | * resources to be freed. 1737 | * 1738 | * error() will return EINTR if wait() is interrupted by a signal. 1739 | * 1740 | * @param nohang true to return immediately if the process has not exited. 1741 | * @return 1 if the process has exited and wait() has not yet been called. 1742 | * 0 if @a nohang is true and the process has not exited yet. 1743 | * -1 if no process has been started or if an error occurs, 1744 | * in which case the error can be found using error(). 1745 | */ 1746 | template 1747 | int 1748 | basic_pstreambuf::wait(bool nohang) 1749 | { 1750 | int child_exited = -1; 1751 | if (is_open()) 1752 | { 1753 | int exit_status; 1754 | switch(::waitpid(ppid_, &exit_status, nohang ? WNOHANG : 0)) 1755 | { 1756 | case 0 : 1757 | // nohang was true and process has not exited 1758 | child_exited = 0; 1759 | break; 1760 | case -1 : 1761 | error_ = errno; 1762 | break; 1763 | default : 1764 | // process has exited 1765 | ppid_ = 0; 1766 | status_ = exit_status; 1767 | child_exited = 1; 1768 | // Close wpipe, would get SIGPIPE if we used it. 1769 | destroy_buffers(pstdin); 1770 | close_fd(wpipe_); 1771 | // Must free read buffers and pipes on destruction 1772 | // or next call to open()/close() 1773 | break; 1774 | } 1775 | } 1776 | return child_exited; 1777 | } 1778 | 1779 | /** 1780 | * Sends the specified signal to the process. A signal can be used to 1781 | * terminate a child process that would not exit otherwise. 1782 | * 1783 | * If an error occurs the error code will be set to one of the possible 1784 | * errors for @c kill(). See your system's documentation for these errors. 1785 | * 1786 | * @param signal A signal to send to the child process. 1787 | * @return @c this or @c NULL if @c kill() fails. 1788 | */ 1789 | template 1790 | inline basic_pstreambuf* 1791 | basic_pstreambuf::kill(int signal) 1792 | { 1793 | basic_pstreambuf* ret = NULL; 1794 | if (is_open()) 1795 | { 1796 | if (::kill(ppid_, signal)) 1797 | error_ = errno; 1798 | else 1799 | { 1800 | #if 0 1801 | // TODO call exited() to check for exit and clean up? leave to user? 1802 | if (signal==SIGTERM || signal==SIGKILL) 1803 | this->exited(); 1804 | #endif 1805 | ret = this; 1806 | } 1807 | } 1808 | return ret; 1809 | } 1810 | 1811 | /** 1812 | * Sends the specified signal to the process group of the child process. 1813 | * A signal can be used to terminate a child process that would not exit 1814 | * otherwise, or to kill the process and its own children. 1815 | * 1816 | * If an error occurs the error code will be set to one of the possible 1817 | * errors for @c getpgid() or @c kill(). See your system's documentation 1818 | * for these errors. If the child is in the current process group then 1819 | * NULL will be returned and the error code set to EPERM. 1820 | * 1821 | * @param signal A signal to send to the child process. 1822 | * @return @c this on success or @c NULL on failure. 1823 | */ 1824 | template 1825 | inline basic_pstreambuf* 1826 | basic_pstreambuf::killpg(int signal) 1827 | { 1828 | basic_pstreambuf* ret = NULL; 1829 | #ifdef _POSIX_JOB_CONTROL 1830 | if (is_open()) 1831 | { 1832 | pid_t pgid = ::getpgid(ppid_); 1833 | if (pgid == -1) 1834 | error_ = errno; 1835 | else if (pgid == ::getpgrp()) 1836 | error_ = EPERM; // Don't commit suicide 1837 | else if (::killpg(pgid, signal)) 1838 | error_ = errno; 1839 | else 1840 | ret = this; 1841 | } 1842 | #else 1843 | error_ = ENOTSUP; 1844 | #endif 1845 | return ret; 1846 | } 1847 | 1848 | /** 1849 | * This function can call pstreambuf::wait() and so may change the 1850 | * object's state if the child process has already exited. 1851 | * 1852 | * @return True if the associated process has exited, false otherwise. 1853 | * @see basic_pstreambuf::wait() 1854 | */ 1855 | template 1856 | inline bool 1857 | basic_pstreambuf::exited() 1858 | { 1859 | return ppid_ == 0 || wait(true)==1; 1860 | } 1861 | 1862 | 1863 | /** 1864 | * @return The exit status of the child process, or -1 if wait() 1865 | * has not yet been called to wait for the child to exit. 1866 | * @see basic_pstreambuf::wait() 1867 | */ 1868 | template 1869 | inline int 1870 | basic_pstreambuf::status() const 1871 | { 1872 | return status_; 1873 | } 1874 | 1875 | /** 1876 | * @return The error code of the most recently failed operation, or zero. 1877 | */ 1878 | template 1879 | inline int 1880 | basic_pstreambuf::error() const 1881 | { 1882 | return error_; 1883 | } 1884 | 1885 | /** 1886 | * Closes the output pipe, causing the child process to receive the 1887 | * end-of-file indicator on subsequent reads from its @c stdin stream. 1888 | */ 1889 | template 1890 | inline void 1891 | basic_pstreambuf::peof() 1892 | { 1893 | sync(); 1894 | destroy_buffers(pstdin); 1895 | close_fd(wpipe_); 1896 | } 1897 | 1898 | /** 1899 | * Unlike pstreambuf::exited(), this function will not call wait() and 1900 | * so will not change the object's state. This means that once a child 1901 | * process is executed successfully this function will continue to 1902 | * return true even after the process exits (until wait() is called.) 1903 | * 1904 | * @return true if a previous call to open() succeeded and wait() has 1905 | * not been called and determined that the process has exited, 1906 | * false otherwise. 1907 | */ 1908 | template 1909 | inline bool 1910 | basic_pstreambuf::is_open() const 1911 | { 1912 | return ppid_ > 0; 1913 | } 1914 | 1915 | /** 1916 | * Toggle the stream used for reading. If @a readerr is @c true then the 1917 | * process' @c stderr output will be used for subsequent extractions, if 1918 | * @a readerr is false the the process' stdout will be used. 1919 | * @param readerr @c true to read @c stderr, @c false to read @c stdout. 1920 | * @return @c true if the requested stream is open and will be used for 1921 | * subsequent extractions, @c false otherwise. 1922 | */ 1923 | template 1924 | inline bool 1925 | basic_pstreambuf::read_err(bool readerr) 1926 | { 1927 | buf_read_src src = readerr ? rsrc_err : rsrc_out; 1928 | if (rpipe_[src]>=0) 1929 | { 1930 | switch_read_buffer(src); 1931 | return true; 1932 | } 1933 | return false; 1934 | } 1935 | 1936 | /** 1937 | * Called when the internal character buffer is not present or is full, 1938 | * to transfer the buffer contents to the pipe. 1939 | * 1940 | * @param c a character to be written to the pipe. 1941 | * @return @c traits_type::eof() if an error occurs, otherwise if @a c 1942 | * is not equal to @c traits_type::eof() it will be buffered and 1943 | * a value other than @c traits_type::eof() returned to indicate 1944 | * success. 1945 | */ 1946 | template 1947 | typename basic_pstreambuf::int_type 1948 | basic_pstreambuf::overflow(int_type c) 1949 | { 1950 | if (!empty_buffer()) 1951 | return traits_type::eof(); 1952 | else if (!traits_type::eq_int_type(c, traits_type::eof())) 1953 | return this->sputc(c); 1954 | else 1955 | return traits_type::not_eof(c); 1956 | } 1957 | 1958 | 1959 | template 1960 | int 1961 | basic_pstreambuf::sync() 1962 | { 1963 | return !exited() && empty_buffer() ? 0 : -1; 1964 | } 1965 | 1966 | /** 1967 | * @param s character buffer. 1968 | * @param n buffer length. 1969 | * @return the number of characters written. 1970 | */ 1971 | template 1972 | std::streamsize 1973 | basic_pstreambuf::xsputn(const char_type* s, std::streamsize n) 1974 | { 1975 | std::streamsize done = 0; 1976 | while (done < n) 1977 | { 1978 | if (std::streamsize nbuf = this->epptr() - this->pptr()) 1979 | { 1980 | nbuf = std::min(nbuf, n - done); 1981 | traits_type::copy(this->pptr(), s + done, nbuf); 1982 | this->pbump(nbuf); 1983 | done += nbuf; 1984 | } 1985 | else if (!empty_buffer()) 1986 | break; 1987 | } 1988 | return done; 1989 | } 1990 | 1991 | /** 1992 | * @return true if the buffer was emptied, false otherwise. 1993 | */ 1994 | template 1995 | bool 1996 | basic_pstreambuf::empty_buffer() 1997 | { 1998 | const std::streamsize count = this->pptr() - this->pbase(); 1999 | if (count > 0) 2000 | { 2001 | const std::streamsize written = this->write(this->wbuffer_, count); 2002 | if (written > 0) 2003 | { 2004 | if (const std::streamsize unwritten = count - written) 2005 | traits_type::move(this->pbase(), this->pbase()+written, unwritten); 2006 | this->pbump(-written); 2007 | return true; 2008 | } 2009 | } 2010 | return false; 2011 | } 2012 | 2013 | /** 2014 | * Called when the internal character buffer is is empty, to re-fill it 2015 | * from the pipe. 2016 | * 2017 | * @return The first available character in the buffer, 2018 | * or @c traits_type::eof() in case of failure. 2019 | */ 2020 | template 2021 | typename basic_pstreambuf::int_type 2022 | basic_pstreambuf::underflow() 2023 | { 2024 | if (this->gptr() < this->egptr() || fill_buffer()) 2025 | return traits_type::to_int_type(*this->gptr()); 2026 | else 2027 | return traits_type::eof(); 2028 | } 2029 | 2030 | /** 2031 | * Attempts to make @a c available as the next character to be read by 2032 | * @c sgetc(). 2033 | * 2034 | * @param c a character to make available for extraction. 2035 | * @return @a c if the character can be made available, 2036 | * @c traits_type::eof() otherwise. 2037 | */ 2038 | template 2039 | typename basic_pstreambuf::int_type 2040 | basic_pstreambuf::pbackfail(int_type c) 2041 | { 2042 | if (this->gptr() != this->eback()) 2043 | { 2044 | this->gbump(-1); 2045 | if (!traits_type::eq_int_type(c, traits_type::eof())) 2046 | *this->gptr() = traits_type::to_char_type(c); 2047 | return traits_type::not_eof(c); 2048 | } 2049 | else 2050 | return traits_type::eof(); 2051 | } 2052 | 2053 | template 2054 | std::streamsize 2055 | basic_pstreambuf::showmanyc() 2056 | { 2057 | int avail = 0; 2058 | if (sizeof(char_type) == 1) 2059 | avail = fill_buffer(true) ? this->egptr() - this->gptr() : -1; 2060 | #ifdef FIONREAD 2061 | else 2062 | { 2063 | if (::ioctl(rpipe(), FIONREAD, &avail) == -1) 2064 | avail = -1; 2065 | else if (avail) 2066 | avail /= sizeof(char_type); 2067 | } 2068 | #endif 2069 | return std::streamsize(avail); 2070 | } 2071 | 2072 | /** 2073 | * @return true if the buffer was filled, false otherwise. 2074 | */ 2075 | template 2076 | bool 2077 | basic_pstreambuf::fill_buffer(bool non_blocking) 2078 | { 2079 | const std::streamsize pb1 = this->gptr() - this->eback(); 2080 | const std::streamsize pb2 = pbsz; 2081 | const std::streamsize npb = std::min(pb1, pb2); 2082 | 2083 | char_type* const rbuf = rbuffer(); 2084 | 2085 | if (npb) 2086 | traits_type::move(rbuf + pbsz - npb, this->gptr() - npb, npb); 2087 | 2088 | std::streamsize rc = -1; 2089 | 2090 | if (non_blocking) 2091 | { 2092 | const int flags = ::fcntl(rpipe(), F_GETFL); 2093 | if (flags != -1) 2094 | { 2095 | const bool blocking = !(flags & O_NONBLOCK); 2096 | if (blocking) 2097 | ::fcntl(rpipe(), F_SETFL, flags | O_NONBLOCK); // set non-blocking 2098 | 2099 | error_ = 0; 2100 | rc = read(rbuf + pbsz, bufsz - pbsz); 2101 | 2102 | if (rc == -1 && error_ == EAGAIN) // nothing available 2103 | rc = 0; 2104 | else if (rc == 0) // EOF 2105 | rc = -1; 2106 | 2107 | if (blocking) 2108 | ::fcntl(rpipe(), F_SETFL, flags); // restore 2109 | } 2110 | } 2111 | else 2112 | rc = read(rbuf + pbsz, bufsz - pbsz); 2113 | 2114 | if (rc > 0 || (rc == 0 && non_blocking)) 2115 | { 2116 | this->setg( rbuf + pbsz - npb, 2117 | rbuf + pbsz, 2118 | rbuf + pbsz + rc ); 2119 | return true; 2120 | } 2121 | else 2122 | { 2123 | this->setg(NULL, NULL, NULL); 2124 | return false; 2125 | } 2126 | } 2127 | 2128 | /** 2129 | * Writes up to @a n characters to the pipe from the buffer @a s. 2130 | * 2131 | * @param s character buffer. 2132 | * @param n buffer length. 2133 | * @return the number of characters written. 2134 | */ 2135 | template 2136 | inline std::streamsize 2137 | basic_pstreambuf::write(const char_type* s, std::streamsize n) 2138 | { 2139 | std::streamsize nwritten = 0; 2140 | if (wpipe() >= 0) 2141 | { 2142 | nwritten = ::write(wpipe(), s, n * sizeof(char_type)); 2143 | if (nwritten == -1) 2144 | error_ = errno; 2145 | else 2146 | nwritten /= sizeof(char_type); 2147 | } 2148 | return nwritten; 2149 | } 2150 | 2151 | /** 2152 | * Reads up to @a n characters from the pipe to the buffer @a s. 2153 | * 2154 | * @param s character buffer. 2155 | * @param n buffer length. 2156 | * @return the number of characters read. 2157 | */ 2158 | template 2159 | inline std::streamsize 2160 | basic_pstreambuf::read(char_type* s, std::streamsize n) 2161 | { 2162 | std::streamsize nread = 0; 2163 | if (rpipe() >= 0) 2164 | { 2165 | nread = ::read(rpipe(), s, n * sizeof(char_type)); 2166 | if (nread == -1) 2167 | error_ = errno; 2168 | else 2169 | nread /= sizeof(char_type); 2170 | } 2171 | return nread; 2172 | } 2173 | 2174 | /** @return a reference to the output file descriptor */ 2175 | template 2176 | inline pstreams::fd_type& 2177 | basic_pstreambuf::wpipe() 2178 | { 2179 | return wpipe_; 2180 | } 2181 | 2182 | /** @return a reference to the active input file descriptor */ 2183 | template 2184 | inline pstreams::fd_type& 2185 | basic_pstreambuf::rpipe() 2186 | { 2187 | return rpipe_[rsrc_]; 2188 | } 2189 | 2190 | /** @return a reference to the specified input file descriptor */ 2191 | template 2192 | inline pstreams::fd_type& 2193 | basic_pstreambuf::rpipe(buf_read_src which) 2194 | { 2195 | return rpipe_[which]; 2196 | } 2197 | 2198 | /** @return a pointer to the start of the active input buffer area. */ 2199 | template 2200 | inline typename basic_pstreambuf::char_type* 2201 | basic_pstreambuf::rbuffer() 2202 | { 2203 | return rbuffer_[rsrc_]; 2204 | } 2205 | 2206 | 2207 | /* 2208 | * member definitions for pstream_common 2209 | */ 2210 | 2211 | /** 2212 | * @class pstream_common 2213 | * Abstract Base Class providing common functionality for basic_ipstream, 2214 | * basic_opstream and basic_pstream. 2215 | * pstream_common manages the basic_pstreambuf stream buffer that is used 2216 | * by the derived classes to initialise an iostream class. 2217 | */ 2218 | 2219 | /** Creates an uninitialised stream. */ 2220 | template 2221 | inline 2222 | pstream_common::pstream_common() 2223 | : std::basic_ios(NULL) 2224 | , command_() 2225 | , buf_() 2226 | { 2227 | this->std::basic_ios::rdbuf(&buf_); 2228 | } 2229 | 2230 | /** 2231 | * Initialises the stream buffer by calling 2232 | * do_open( @a command , @a mode ) 2233 | * 2234 | * @param cmd a string containing a shell command. 2235 | * @param mode the I/O mode to use when opening the pipe. 2236 | * @see do_open(const std::string&, pmode) 2237 | */ 2238 | template 2239 | inline 2240 | pstream_common::pstream_common(const std::string& cmd, pmode mode) 2241 | : std::basic_ios(NULL) 2242 | , command_(cmd) 2243 | , buf_() 2244 | { 2245 | this->std::basic_ios::rdbuf(&buf_); 2246 | do_open(cmd, mode); 2247 | } 2248 | 2249 | /** 2250 | * Initialises the stream buffer by calling 2251 | * do_open( @a file , @a argv , @a mode ) 2252 | * 2253 | * @param file a string containing the pathname of a program to execute. 2254 | * @param argv a vector of argument strings passed to the new program. 2255 | * @param mode the I/O mode to use when opening the pipe. 2256 | * @see do_open(const std::string&, const argv_type&, pmode) 2257 | */ 2258 | template 2259 | inline 2260 | pstream_common::pstream_common( const std::string& file, 2261 | const argv_type& argv, 2262 | pmode mode ) 2263 | : std::basic_ios(NULL) 2264 | , command_(file) 2265 | , buf_() 2266 | { 2267 | this->std::basic_ios::rdbuf(&buf_); 2268 | do_open(file, argv, mode); 2269 | } 2270 | 2271 | /** 2272 | * This is a pure virtual function to make @c pstream_common abstract. 2273 | * Because it is the destructor it will be called by derived classes 2274 | * and so must be defined. It is also protected, to discourage use of 2275 | * the PStreams classes through pointers or references to the base class. 2276 | * 2277 | * @sa If defining a pure virtual seems odd you should read 2278 | * http://www.gotw.ca/gotw/031.htm (and the rest of the site as well!) 2279 | */ 2280 | template 2281 | inline 2282 | pstream_common::~pstream_common() 2283 | { 2284 | } 2285 | 2286 | /** 2287 | * Calls rdbuf()->open( @a command , @a mode ) 2288 | * and sets @c failbit on error. 2289 | * 2290 | * @param cmd a string containing a shell command. 2291 | * @param mode the I/O mode to use when opening the pipe. 2292 | * @see basic_pstreambuf::open(const std::string&, pmode) 2293 | */ 2294 | template 2295 | inline void 2296 | pstream_common::do_open(const std::string& cmd, pmode mode) 2297 | { 2298 | if (!buf_.open((command_=cmd), mode)) 2299 | this->setstate(std::ios_base::failbit); 2300 | } 2301 | 2302 | /** 2303 | * Calls rdbuf()->open( @a file, @a argv, @a mode ) 2304 | * and sets @c failbit on error. 2305 | * 2306 | * @param file a string containing the pathname of a program to execute. 2307 | * @param argv a vector of argument strings passed to the new program. 2308 | * @param mode the I/O mode to use when opening the pipe. 2309 | * @see basic_pstreambuf::open(const std::string&, const argv_type&, pmode) 2310 | */ 2311 | template 2312 | inline void 2313 | pstream_common::do_open( const std::string& file, 2314 | const argv_type& argv, 2315 | pmode mode ) 2316 | { 2317 | if (!buf_.open((command_=file), argv, mode)) 2318 | this->setstate(std::ios_base::failbit); 2319 | } 2320 | 2321 | /** Calls rdbuf->close() and sets @c failbit on error. Returns 2322 | * process's exit status, as pclose(3) does. */ 2323 | template 2324 | inline int 2325 | pstream_common::close() 2326 | { 2327 | if (!buf_.close()) 2328 | this->setstate(std::ios_base::failbit); 2329 | return buf_.status(); 2330 | } 2331 | 2332 | /** 2333 | * @return rdbuf()->is_open(). 2334 | * @see basic_pstreambuf::is_open() 2335 | */ 2336 | template 2337 | inline bool 2338 | pstream_common::is_open() const 2339 | { 2340 | return buf_.is_open(); 2341 | } 2342 | 2343 | /** @return a string containing the command used to initialise the stream. */ 2344 | template 2345 | inline const std::string& 2346 | pstream_common::command() const 2347 | { 2348 | return command_; 2349 | } 2350 | 2351 | /** @return a pointer to the private stream buffer member. */ 2352 | // TODO document behaviour if buffer replaced. 2353 | template 2354 | inline typename pstream_common::streambuf_type* 2355 | pstream_common::rdbuf() const 2356 | { 2357 | return const_cast(&buf_); 2358 | } 2359 | 2360 | 2361 | #if REDI_EVISCERATE_PSTREAMS 2362 | /** 2363 | * @def REDI_EVISCERATE_PSTREAMS 2364 | * If this macro has a non-zero value then certain internals of the 2365 | * @c basic_pstreambuf template class are exposed. In general this is 2366 | * a Bad Thing, as the internal implementation is largely undocumented 2367 | * and may be subject to change at any time, so this feature is only 2368 | * provided because it might make PStreams useful in situations where 2369 | * it is necessary to do Bad Things. 2370 | */ 2371 | 2372 | /** 2373 | * @warning This function exposes the internals of the stream buffer and 2374 | * should be used with caution. It is the caller's responsibility 2375 | * to flush streams etc. in order to clear any buffered data. 2376 | * The POSIX.1 function fdopen(3) is used to obtain the 2377 | * @c FILE pointers from the streambuf's private file descriptor 2378 | * members so consult your system's documentation for 2379 | * fdopen(3). 2380 | * 2381 | * @param in A FILE* that will refer to the process' stdin. 2382 | * @param out A FILE* that will refer to the process' stdout. 2383 | * @param err A FILE* that will refer to the process' stderr. 2384 | * @return An OR of zero or more of @c pstdin, @c pstdout, @c pstderr. 2385 | * 2386 | * For each open stream shared with the child process a @c FILE* is 2387 | * obtained and assigned to the corresponding parameter. For closed 2388 | * streams @c NULL is assigned to the parameter. 2389 | * The return value can be tested to see which parameters should be 2390 | * @c !NULL by masking with the corresponding @c pmode value. 2391 | * 2392 | * @see fdopen(3) 2393 | */ 2394 | template 2395 | std::size_t 2396 | basic_pstreambuf::fopen(FILE*& in, FILE*& out, FILE*& err) 2397 | { 2398 | in = out = err = NULL; 2399 | std::size_t open_files = 0; 2400 | if (wpipe() > -1) 2401 | { 2402 | if ((in = ::fdopen(wpipe(), "w"))) 2403 | { 2404 | open_files |= pstdin; 2405 | } 2406 | } 2407 | if (rpipe(rsrc_out) > -1) 2408 | { 2409 | if ((out = ::fdopen(rpipe(rsrc_out), "r"))) 2410 | { 2411 | open_files |= pstdout; 2412 | } 2413 | } 2414 | if (rpipe(rsrc_err) > -1) 2415 | { 2416 | if ((err = ::fdopen(rpipe(rsrc_err), "r"))) 2417 | { 2418 | open_files |= pstderr; 2419 | } 2420 | } 2421 | return open_files; 2422 | } 2423 | 2424 | /** 2425 | * @warning This function exposes the internals of the stream buffer and 2426 | * should be used with caution. 2427 | * 2428 | * @param in A FILE* that will refer to the process' stdin. 2429 | * @param out A FILE* that will refer to the process' stdout. 2430 | * @param err A FILE* that will refer to the process' stderr. 2431 | * @return A bitwise-or of zero or more of @c pstdin, @c pstdout, @c pstderr. 2432 | * @see basic_pstreambuf::fopen() 2433 | */ 2434 | template 2435 | inline std::size_t 2436 | pstream_common::fopen(FILE*& fin, FILE*& fout, FILE*& ferr) 2437 | { 2438 | return buf_.fopen(fin, fout, ferr); 2439 | } 2440 | 2441 | #endif // REDI_EVISCERATE_PSTREAMS 2442 | 2443 | 2444 | } // namespace redi 2445 | 2446 | /** 2447 | * @mainpage PStreams Reference 2448 | * @htmlinclude mainpage.html 2449 | */ 2450 | 2451 | #endif // REDI_PSTREAM_H_SEEN 2452 | 2453 | // vim: ts=2 sw=2 expandtab 2454 | 2455 | --------------------------------------------------------------------------------