1 /*
2 
3 This is a generic program misc functions class v1.0.
4 Copyright � 1997,2002,2003,2004,2008,2010 Jaakko Hyv�tti
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see http://www.gnu.org/licenses/ .
18 
19 The author may be contacted at:
20 
21 Email: Jaakko.Hyvatti@iki.fi
22 URL:   http://www.iki.fi/hyvatti/
23 Phone: +358 40 5011222
24 
25 Please send any suggestions, bug reports, success stories etc. to the
26 Email address above.  Include word 'picprog' in the subject line to
27 make sure your email passes my spam filtering.
28 
29 */
30 
31 #include <cstdlib>
32 #include <cstring>
33 #include <iostream>
34 #include <string>
35 
36 #include <unistd.h>
37 #include <sysexits.h>
38 #include <sys/ioctl.h>
39 #include <termios.h>
40 
41 #include "program.h"
42 
43 using namespace std;
44 
program()45 program::program ()
46 {
47   name = 0;
48 }
49 
~program()50 program::~program ()
51 {
52   if (name)
53     delete[] name;
54 }
55 
init(char * argv[])56 void program::init (char *argv [])
57 {
58   char *p;
59 
60   if (argv && argv [0] && argv [0][0]) {
61     if (name)
62       delete[] name;
63     if ((p = strrchr (argv [0], '/')))
64       p++;
65     else
66       p = argv [0];
67     name = new char [strlen (p)+1];
68     strcpy (name, p);
69   }
70 }
71 
72 const char program::copying_t [] =
73 "                    GNU GENERAL PUBLIC LICENSE\n\
74                        Version 3, 29 June 2007\n\
75 \n\
76  Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\n\
77  Everyone is permitted to copy and distribute verbatim copies\n\
78  of this license document, but changing it is not allowed.\n\
79 \n\
80                             Preamble\n\
81 \n\
82   The GNU General Public License is a free, copyleft license for\n\
83 software and other kinds of works.\n\
84 \n\
85   The licenses for most software and other practical works are designed\n\
86 to take away your freedom to share and change the works.  By contrast,\n\
87 the GNU General Public License is intended to guarantee your freedom to\n\
88 share and change all versions of a program--to make sure it remains free\n\
89 software for all its users.  We, the Free Software Foundation, use the\n\
90 GNU General Public License for most of our software; it applies also to\n\
91 any other work released this way by its authors.  You can apply it to\n\
92 your programs, too.\n\
93 \n\
94   When we speak of free software, we are referring to freedom, not\n\
95 price.  Our General Public Licenses are designed to make sure that you\n\
96 have the freedom to distribute copies of free software (and charge for\n\
97 them if you wish), that you receive source code or can get it if you\n\
98 want it, that you can change the software or use pieces of it in new\n\
99 free programs, and that you know you can do these things.\n\
100 \n\
101   To protect your rights, we need to prevent others from denying you\n\
102 these rights or asking you to surrender the rights.  Therefore, you have\n\
103 certain responsibilities if you distribute copies of the software, or if\n\
104 you modify it: responsibilities to respect the freedom of others.\n\
105 \n\
106   For example, if you distribute copies of such a program, whether\n\
107 gratis or for a fee, you must pass on to the recipients the same\n\
108 freedoms that you received.  You must make sure that they, too, receive\n\
109 or can get the source code.  And you must show them these terms so they\n\
110 know their rights.\n\
111 \n\
112   Developers that use the GNU GPL protect your rights with two steps:\n\
113 (1) assert copyright on the software, and (2) offer you this License\n\
114 giving you legal permission to copy, distribute and/or modify it.\n\
115 \n\
116   For the developers' and authors' protection, the GPL clearly explains\n\
117 that there is no warranty for this free software.  For both users' and\n\
118 authors' sake, the GPL requires that modified versions be marked as\n\
119 changed, so that their problems will not be attributed erroneously to\n\
120 authors of previous versions.\n\
121 \n\
122   Some devices are designed to deny users access to install or run\n\
123 modified versions of the software inside them, although the manufacturer\n\
124 can do so.  This is fundamentally incompatible with the aim of\n\
125 protecting users' freedom to change the software.  The systematic\n\
126 pattern of such abuse occurs in the area of products for individuals to\n\
127 use, which is precisely where it is most unacceptable.  Therefore, we\n\
128 have designed this version of the GPL to prohibit the practice for those\n\
129 products.  If such problems arise substantially in other domains, we\n\
130 stand ready to extend this provision to those domains in future versions\n\
131 of the GPL, as needed to protect the freedom of users.\n\
132 \n\
133   Finally, every program is threatened constantly by software patents.\n\
134 States should not allow patents to restrict development and use of\n\
135 software on general-purpose computers, but in those that do, we wish to\n\
136 avoid the special danger that patents applied to a free program could\n\
137 make it effectively proprietary.  To prevent this, the GPL assures that\n\
138 patents cannot be used to render the program non-free.\n\
139 \n\
140   The precise terms and conditions for copying, distribution and\n\
141 modification follow.\n\
142 \n\
143                        TERMS AND CONDITIONS\n\
144 \n\
145   0. Definitions.\n\
146 \n\
147   \"This License\" refers to version 3 of the GNU General Public License.\n\
148 \n\
149   \"Copyright\" also means copyright-like laws that apply to other kinds of\n\
150 works, such as semiconductor masks.\n\
151 \n\
152   \"The Program\" refers to any copyrightable work licensed under this\n\
153 License.  Each licensee is addressed as \"you\".  \"Licensees\" and\n\
154 \"recipients\" may be individuals or organizations.\n\
155 \n\
156   To \"modify\" a work means to copy from or adapt all or part of the work\n\
157 in a fashion requiring copyright permission, other than the making of an\n\
158 exact copy.  The resulting work is called a \"modified version\" of the\n\
159 earlier work or a work \"based on\" the earlier work.\n\
160 \n\
161   A \"covered work\" means either the unmodified Program or a work based\n\
162 on the Program.\n\
163 \n\
164   To \"propagate\" a work means to do anything with it that, without\n\
165 permission, would make you directly or secondarily liable for\n\
166 infringement under applicable copyright law, except executing it on a\n\
167 computer or modifying a private copy.  Propagation includes copying,\n\
168 distribution (with or without modification), making available to the\n\
169 public, and in some countries other activities as well.\n\
170 \n\
171   To \"convey\" a work means any kind of propagation that enables other\n\
172 parties to make or receive copies.  Mere interaction with a user through\n\
173 a computer network, with no transfer of a copy, is not conveying.\n\
174 \n\
175   An interactive user interface displays \"Appropriate Legal Notices\"\n\
176 to the extent that it includes a convenient and prominently visible\n\
177 feature that (1) displays an appropriate copyright notice, and (2)\n\
178 tells the user that there is no warranty for the work (except to the\n\
179 extent that warranties are provided), that licensees may convey the\n\
180 work under this License, and how to view a copy of this License.  If\n\
181 the interface presents a list of user commands or options, such as a\n\
182 menu, a prominent item in the list meets this criterion.\n\
183 \n\
184   1. Source Code.\n\
185 \n\
186   The \"source code\" for a work means the preferred form of the work\n\
187 for making modifications to it.  \"Object code\" means any non-source\n\
188 form of a work.\n\
189 \n\
190   A \"Standard Interface\" means an interface that either is an official\n\
191 standard defined by a recognized standards body, or, in the case of\n\
192 interfaces specified for a particular programming language, one that\n\
193 is widely used among developers working in that language.\n\
194 \n\
195   The \"System Libraries\" of an executable work include anything, other\n\
196 than the work as a whole, that (a) is included in the normal form of\n\
197 packaging a Major Component, but which is not part of that Major\n\
198 Component, and (b) serves only to enable use of the work with that\n\
199 Major Component, or to implement a Standard Interface for which an\n\
200 implementation is available to the public in source code form.  A\n\
201 \"Major Component\", in this context, means a major essential component\n\
202 (kernel, window system, and so on) of the specific operating system\n\
203 (if any) on which the executable work runs, or a compiler used to\n\
204 produce the work, or an object code interpreter used to run it.\n\
205 \n\
206   The \"Corresponding Source\" for a work in object code form means all\n\
207 the source code needed to generate, install, and (for an executable\n\
208 work) run the object code and to modify the work, including scripts to\n\
209 control those activities.  However, it does not include the work's\n\
210 System Libraries, or general-purpose tools or generally available free\n\
211 programs which are used unmodified in performing those activities but\n\
212 which are not part of the work.  For example, Corresponding Source\n\
213 includes interface definition files associated with source files for\n\
214 the work, and the source code for shared libraries and dynamically\n\
215 linked subprograms that the work is specifically designed to require,\n\
216 such as by intimate data communication or control flow between those\n\
217 subprograms and other parts of the work.\n\
218 \n\
219   The Corresponding Source need not include anything that users\n\
220 can regenerate automatically from other parts of the Corresponding\n\
221 Source.\n\
222 \n\
223   The Corresponding Source for a work in source code form is that\n\
224 same work.\n\
225 \n\
226   2. Basic Permissions.\n\
227 \n\
228   All rights granted under this License are granted for the term of\n\
229 copyright on the Program, and are irrevocable provided the stated\n\
230 conditions are met.  This License explicitly affirms your unlimited\n\
231 permission to run the unmodified Program.  The output from running a\n\
232 covered work is covered by this License only if the output, given its\n\
233 content, constitutes a covered work.  This License acknowledges your\n\
234 rights of fair use or other equivalent, as provided by copyright law.\n\
235 \n\
236   You may make, run and propagate covered works that you do not\n\
237 convey, without conditions so long as your license otherwise remains\n\
238 in force.  You may convey covered works to others for the sole purpose\n\
239 of having them make modifications exclusively for you, or provide you\n\
240 with facilities for running those works, provided that you comply with\n\
241 the terms of this License in conveying all material for which you do\n\
242 not control copyright.  Those thus making or running the covered works\n\
243 for you must do so exclusively on your behalf, under your direction\n\
244 and control, on terms that prohibit them from making any copies of\n\
245 your copyrighted material outside their relationship with you.\n\
246 \n\
247   Conveying under any other circumstances is permitted solely under\n\
248 the conditions stated below.  Sublicensing is not allowed; section 10\n\
249 makes it unnecessary.\n\
250 \n\
251   3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\
252 \n\
253   No covered work shall be deemed part of an effective technological\n\
254 measure under any applicable law fulfilling obligations under article\n\
255 11 of the WIPO copyright treaty adopted on 20 December 1996, or\n\
256 similar laws prohibiting or restricting circumvention of such\n\
257 measures.\n\
258 \n\
259   When you convey a covered work, you waive any legal power to forbid\n\
260 circumvention of technological measures to the extent such circumvention\n\
261 is effected by exercising rights under this License with respect to\n\
262 the covered work, and you disclaim any intention to limit operation or\n\
263 modification of the work as a means of enforcing, against the work's\n\
264 users, your or third parties' legal rights to forbid circumvention of\n\
265 technological measures.\n\
266 \n\
267   4. Conveying Verbatim Copies.\n\
268 \n\
269   You may convey verbatim copies of the Program's source code as you\n\
270 receive it, in any medium, provided that you conspicuously and\n\
271 appropriately publish on each copy an appropriate copyright notice;\n\
272 keep intact all notices stating that this License and any\n\
273 non-permissive terms added in accord with section 7 apply to the code;\n\
274 keep intact all notices of the absence of any warranty; and give all\n\
275 recipients a copy of this License along with the Program.\n\
276 \n\
277   You may charge any price or no price for each copy that you convey,\n\
278 and you may offer support or warranty protection for a fee.\n\
279 \n\
280   5. Conveying Modified Source Versions.\n\
281 \n\
282   You may convey a work based on the Program, or the modifications to\n\
283 produce it from the Program, in the form of source code under the\n\
284 terms of section 4, provided that you also meet all of these conditions:\n\
285 \n\
286     a) The work must carry prominent notices stating that you modified\n\
287     it, and giving a relevant date.\n\
288 \n\
289     b) The work must carry prominent notices stating that it is\n\
290     released under this License and any conditions added under section\n\
291     7.  This requirement modifies the requirement in section 4 to\n\
292     \"keep intact all notices\".\n\
293 \n\
294     c) You must license the entire work, as a whole, under this\n\
295     License to anyone who comes into possession of a copy.  This\n\
296     License will therefore apply, along with any applicable section 7\n\
297     additional terms, to the whole of the work, and all its parts,\n\
298     regardless of how they are packaged.  This License gives no\n\
299     permission to license the work in any other way, but it does not\n\
300     invalidate such permission if you have separately received it.\n\
301 \n\
302     d) If the work has interactive user interfaces, each must display\n\
303     Appropriate Legal Notices; however, if the Program has interactive\n\
304     interfaces that do not display Appropriate Legal Notices, your\n\
305     work need not make them do so.\n\
306 \n\
307   A compilation of a covered work with other separate and independent\n\
308 works, which are not by their nature extensions of the covered work,\n\
309 and which are not combined with it such as to form a larger program,\n\
310 in or on a volume of a storage or distribution medium, is called an\n\
311 \"aggregate\" if the compilation and its resulting copyright are not\n\
312 used to limit the access or legal rights of the compilation's users\n\
313 beyond what the individual works permit.  Inclusion of a covered work\n\
314 in an aggregate does not cause this License to apply to the other\n\
315 parts of the aggregate.\n\
316 \n\
317   6. Conveying Non-Source Forms.\n\
318 \n\
319   You may convey a covered work in object code form under the terms\n\
320 of sections 4 and 5, provided that you also convey the\n\
321 machine-readable Corresponding Source under the terms of this License,\n\
322 in one of these ways:\n\
323 \n\
324     a) Convey the object code in, or embodied in, a physical product\n\
325     (including a physical distribution medium), accompanied by the\n\
326     Corresponding Source fixed on a durable physical medium\n\
327     customarily used for software interchange.\n\
328 \n\
329     b) Convey the object code in, or embodied in, a physical product\n\
330     (including a physical distribution medium), accompanied by a\n\
331     written offer, valid for at least three years and valid for as\n\
332     long as you offer spare parts or customer support for that product\n\
333     model, to give anyone who possesses the object code either (1) a\n\
334     copy of the Corresponding Source for all the software in the\n\
335     product that is covered by this License, on a durable physical\n\
336     medium customarily used for software interchange, for a price no\n\
337     more than your reasonable cost of physically performing this\n\
338     conveying of source, or (2) access to copy the\n\
339     Corresponding Source from a network server at no charge.\n\
340 \n\
341     c) Convey individual copies of the object code with a copy of the\n\
342     written offer to provide the Corresponding Source.  This\n\
343     alternative is allowed only occasionally and noncommercially, and\n\
344     only if you received the object code with such an offer, in accord\n\
345     with subsection 6b.\n\
346 \n\
347     d) Convey the object code by offering access from a designated\n\
348     place (gratis or for a charge), and offer equivalent access to the\n\
349     Corresponding Source in the same way through the same place at no\n\
350     further charge.  You need not require recipients to copy the\n\
351     Corresponding Source along with the object code.  If the place to\n\
352     copy the object code is a network server, the Corresponding Source\n\
353     may be on a different server (operated by you or a third party)\n\
354     that supports equivalent copying facilities, provided you maintain\n\
355     clear directions next to the object code saying where to find the\n\
356     Corresponding Source.  Regardless of what server hosts the\n\
357     Corresponding Source, you remain obligated to ensure that it is\n\
358     available for as long as needed to satisfy these requirements.\n\
359 \n\
360     e) Convey the object code using peer-to-peer transmission, provided\n\
361     you inform other peers where the object code and Corresponding\n\
362     Source of the work are being offered to the general public at no\n\
363     charge under subsection 6d.\n\
364 \n\
365   A separable portion of the object code, whose source code is excluded\n\
366 from the Corresponding Source as a System Library, need not be\n\
367 included in conveying the object code work.\n\
368 \n\
369   A \"User Product\" is either (1) a \"consumer product\", which means any\n\
370 tangible personal property which is normally used for personal, family,\n\
371 or household purposes, or (2) anything designed or sold for incorporation\n\
372 into a dwelling.  In determining whether a product is a consumer product,\n\
373 doubtful cases shall be resolved in favor of coverage.  For a particular\n\
374 product received by a particular user, \"normally used\" refers to a\n\
375 typical or common use of that class of product, regardless of the status\n\
376 of the particular user or of the way in which the particular user\n\
377 actually uses, or expects or is expected to use, the product.  A product\n\
378 is a consumer product regardless of whether the product has substantial\n\
379 commercial, industrial or non-consumer uses, unless such uses represent\n\
380 the only significant mode of use of the product.\n\
381 \n\
382   \"Installation Information\" for a User Product means any methods,\n\
383 procedures, authorization keys, or other information required to install\n\
384 and execute modified versions of a covered work in that User Product from\n\
385 a modified version of its Corresponding Source.  The information must\n\
386 suffice to ensure that the continued functioning of the modified object\n\
387 code is in no case prevented or interfered with solely because\n\
388 modification has been made.\n\
389 \n\
390   If you convey an object code work under this section in, or with, or\n\
391 specifically for use in, a User Product, and the conveying occurs as\n\
392 part of a transaction in which the right of possession and use of the\n\
393 User Product is transferred to the recipient in perpetuity or for a\n\
394 fixed term (regardless of how the transaction is characterized), the\n\
395 Corresponding Source conveyed under this section must be accompanied\n\
396 by the Installation Information.  But this requirement does not apply\n\
397 if neither you nor any third party retains the ability to install\n\
398 modified object code on the User Product (for example, the work has\n\
399 been installed in ROM).\n\
400 \n\
401   The requirement to provide Installation Information does not include a\n\
402 requirement to continue to provide support service, warranty, or updates\n\
403 for a work that has been modified or installed by the recipient, or for\n\
404 the User Product in which it has been modified or installed.  Access to a\n\
405 network may be denied when the modification itself materially and\n\
406 adversely affects the operation of the network or violates the rules and\n\
407 protocols for communication across the network.\n\
408 \n\
409   Corresponding Source conveyed, and Installation Information provided,\n\
410 in accord with this section must be in a format that is publicly\n\
411 documented (and with an implementation available to the public in\n\
412 source code form), and must require no special password or key for\n\
413 unpacking, reading or copying.\n\
414 \n\
415   7. Additional Terms.\n\
416 \n\
417   \"Additional permissions\" are terms that supplement the terms of this\n\
418 License by making exceptions from one or more of its conditions.\n\
419 Additional permissions that are applicable to the entire Program shall\n\
420 be treated as though they were included in this License, to the extent\n\
421 that they are valid under applicable law.  If additional permissions\n\
422 apply only to part of the Program, that part may be used separately\n\
423 under those permissions, but the entire Program remains governed by\n\
424 this License without regard to the additional permissions.\n\
425 \n\
426   When you convey a copy of a covered work, you may at your option\n\
427 remove any additional permissions from that copy, or from any part of\n\
428 it.  (Additional permissions may be written to require their own\n\
429 removal in certain cases when you modify the work.)  You may place\n\
430 additional permissions on material, added by you to a covered work,\n\
431 for which you have or can give appropriate copyright permission.\n\
432 \n\
433   Notwithstanding any other provision of this License, for material you\n\
434 add to a covered work, you may (if authorized by the copyright holders of\n\
435 that material) supplement the terms of this License with terms:\n\
436 \n\
437     a) Disclaiming warranty or limiting liability differently from the\n\
438     terms of sections 15 and 16 of this License; or\n\
439 \n\
440     b) Requiring preservation of specified reasonable legal notices or\n\
441     author attributions in that material or in the Appropriate Legal\n\
442     Notices displayed by works containing it; or\n\
443 \n\
444     c) Prohibiting misrepresentation of the origin of that material, or\n\
445     requiring that modified versions of such material be marked in\n\
446     reasonable ways as different from the original version; or\n\
447 \n\
448     d) Limiting the use for publicity purposes of names of licensors or\n\
449     authors of the material; or\n\
450 \n\
451     e) Declining to grant rights under trademark law for use of some\n\
452     trade names, trademarks, or service marks; or\n\
453 \n\
454     f) Requiring indemnification of licensors and authors of that\n\
455     material by anyone who conveys the material (or modified versions of\n\
456     it) with contractual assumptions of liability to the recipient, for\n\
457     any liability that these contractual assumptions directly impose on\n\
458     those licensors and authors.\n\
459 \n\
460   All other non-permissive additional terms are considered \"further\n\
461 restrictions\" within the meaning of section 10.  If the Program as you\n\
462 received it, or any part of it, contains a notice stating that it is\n\
463 governed by this License along with a term that is a further\n\
464 restriction, you may remove that term.  If a license document contains\n\
465 a further restriction but permits relicensing or conveying under this\n\
466 License, you may add to a covered work material governed by the terms\n\
467 of that license document, provided that the further restriction does\n\
468 not survive such relicensing or conveying.\n\
469 \n\
470   If you add terms to a covered work in accord with this section, you\n\
471 must place, in the relevant source files, a statement of the\n\
472 additional terms that apply to those files, or a notice indicating\n\
473 where to find the applicable terms.\n\
474 \n\
475   Additional terms, permissive or non-permissive, may be stated in the\n\
476 form of a separately written license, or stated as exceptions;\n\
477 the above requirements apply either way.\n\
478 \n\
479   8. Termination.\n\
480 \n\
481   You may not propagate or modify a covered work except as expressly\n\
482 provided under this License.  Any attempt otherwise to propagate or\n\
483 modify it is void, and will automatically terminate your rights under\n\
484 this License (including any patent licenses granted under the third\n\
485 paragraph of section 11).\n\
486 \n\
487   However, if you cease all violation of this License, then your\n\
488 license from a particular copyright holder is reinstated (a)\n\
489 provisionally, unless and until the copyright holder explicitly and\n\
490 finally terminates your license, and (b) permanently, if the copyright\n\
491 holder fails to notify you of the violation by some reasonable means\n\
492 prior to 60 days after the cessation.\n\
493 \n\
494   Moreover, your license from a particular copyright holder is\n\
495 reinstated permanently if the copyright holder notifies you of the\n\
496 violation by some reasonable means, this is the first time you have\n\
497 received notice of violation of this License (for any work) from that\n\
498 copyright holder, and you cure the violation prior to 30 days after\n\
499 your receipt of the notice.\n\
500 \n\
501   Termination of your rights under this section does not terminate the\n\
502 licenses of parties who have received copies or rights from you under\n\
503 this License.  If your rights have been terminated and not permanently\n\
504 reinstated, you do not qualify to receive new licenses for the same\n\
505 material under section 10.\n\
506 \n\
507   9. Acceptance Not Required for Having Copies.\n\
508 \n\
509   You are not required to accept this License in order to receive or\n\
510 run a copy of the Program.  Ancillary propagation of a covered work\n\
511 occurring solely as a consequence of using peer-to-peer transmission\n\
512 to receive a copy likewise does not require acceptance.  However,\n\
513 nothing other than this License grants you permission to propagate or\n\
514 modify any covered work.  These actions infringe copyright if you do\n\
515 not accept this License.  Therefore, by modifying or propagating a\n\
516 covered work, you indicate your acceptance of this License to do so.\n\
517 \n\
518   10. Automatic Licensing of Downstream Recipients.\n\
519 \n\
520   Each time you convey a covered work, the recipient automatically\n\
521 receives a license from the original licensors, to run, modify and\n\
522 propagate that work, subject to this License.  You are not responsible\n\
523 for enforcing compliance by third parties with this License.\n\
524 \n\
525   An \"entity transaction\" is a transaction transferring control of an\n\
526 organization, or substantially all assets of one, or subdividing an\n\
527 organization, or merging organizations.  If propagation of a covered\n\
528 work results from an entity transaction, each party to that\n\
529 transaction who receives a copy of the work also receives whatever\n\
530 licenses to the work the party's predecessor in interest had or could\n\
531 give under the previous paragraph, plus a right to possession of the\n\
532 Corresponding Source of the work from the predecessor in interest, if\n\
533 the predecessor has it or can get it with reasonable efforts.\n\
534 \n\
535   You may not impose any further restrictions on the exercise of the\n\
536 rights granted or affirmed under this License.  For example, you may\n\
537 not impose a license fee, royalty, or other charge for exercise of\n\
538 rights granted under this License, and you may not initiate litigation\n\
539 (including a cross-claim or counterclaim in a lawsuit) alleging that\n\
540 any patent claim is infringed by making, using, selling, offering for\n\
541 sale, or importing the Program or any portion of it.\n\
542 \n\
543   11. Patents.\n\
544 \n\
545   A \"contributor\" is a copyright holder who authorizes use under this\n\
546 License of the Program or a work on which the Program is based.  The\n\
547 work thus licensed is called the contributor's \"contributor version\".\n\
548 \n\
549   A contributor's \"essential patent claims\" are all patent claims\n\
550 owned or controlled by the contributor, whether already acquired or\n\
551 hereafter acquired, that would be infringed by some manner, permitted\n\
552 by this License, of making, using, or selling its contributor version,\n\
553 but do not include claims that would be infringed only as a\n\
554 consequence of further modification of the contributor version.  For\n\
555 purposes of this definition, \"control\" includes the right to grant\n\
556 patent sublicenses in a manner consistent with the requirements of\n\
557 this License.\n\
558 \n\
559   Each contributor grants you a non-exclusive, worldwide, royalty-free\n\
560 patent license under the contributor's essential patent claims, to\n\
561 make, use, sell, offer for sale, import and otherwise run, modify and\n\
562 propagate the contents of its contributor version.\n\
563 \n\
564   In the following three paragraphs, a \"patent license\" is any express\n\
565 agreement or commitment, however denominated, not to enforce a patent\n\
566 (such as an express permission to practice a patent or covenant not to\n\
567 sue for patent infringement).  To \"grant\" such a patent license to a\n\
568 party means to make such an agreement or commitment not to enforce a\n\
569 patent against the party.\n\
570 \n\
571   If you convey a covered work, knowingly relying on a patent license,\n\
572 and the Corresponding Source of the work is not available for anyone\n\
573 to copy, free of charge and under the terms of this License, through a\n\
574 publicly available network server or other readily accessible means,\n\
575 then you must either (1) cause the Corresponding Source to be so\n\
576 available, or (2) arrange to deprive yourself of the benefit of the\n\
577 patent license for this particular work, or (3) arrange, in a manner\n\
578 consistent with the requirements of this License, to extend the patent\n\
579 license to downstream recipients.  \"Knowingly relying\" means you have\n\
580 actual knowledge that, but for the patent license, your conveying the\n\
581 covered work in a country, or your recipient's use of the covered work\n\
582 in a country, would infringe one or more identifiable patents in that\n\
583 country that you have reason to believe are valid.\n\
584 \n\
585   If, pursuant to or in connection with a single transaction or\n\
586 arrangement, you convey, or propagate by procuring conveyance of, a\n\
587 covered work, and grant a patent license to some of the parties\n\
588 receiving the covered work authorizing them to use, propagate, modify\n\
589 or convey a specific copy of the covered work, then the patent license\n\
590 you grant is automatically extended to all recipients of the covered\n\
591 work and works based on it.\n\
592 \n\
593   A patent license is \"discriminatory\" if it does not include within\n\
594 the scope of its coverage, prohibits the exercise of, or is\n\
595 conditioned on the non-exercise of one or more of the rights that are\n\
596 specifically granted under this License.  You may not convey a covered\n\
597 work if you are a party to an arrangement with a third party that is\n\
598 in the business of distributing software, under which you make payment\n\
599 to the third party based on the extent of your activity of conveying\n\
600 the work, and under which the third party grants, to any of the\n\
601 parties who would receive the covered work from you, a discriminatory\n\
602 patent license (a) in connection with copies of the covered work\n\
603 conveyed by you (or copies made from those copies), or (b) primarily\n\
604 for and in connection with specific products or compilations that\n\
605 contain the covered work, unless you entered into that arrangement,\n\
606 or that patent license was granted, prior to 28 March 2007.\n\
607 \n\
608   Nothing in this License shall be construed as excluding or limiting\n\
609 any implied license or other defenses to infringement that may\n\
610 otherwise be available to you under applicable patent law.\n\
611 \n\
612   12. No Surrender of Others' Freedom.\n\
613 \n\
614   If conditions are imposed on you (whether by court order, agreement or\n\
615 otherwise) that contradict the conditions of this License, they do not\n\
616 excuse you from the conditions of this License.  If you cannot convey a\n\
617 covered work so as to satisfy simultaneously your obligations under this\n\
618 License and any other pertinent obligations, then as a consequence you may\n\
619 not convey it at all.  For example, if you agree to terms that obligate you\n\
620 to collect a royalty for further conveying from those to whom you convey\n\
621 the Program, the only way you could satisfy both those terms and this\n\
622 License would be to refrain entirely from conveying the Program.\n\
623 \n\
624   13. Use with the GNU Affero General Public License.\n\
625 \n\
626   Notwithstanding any other provision of this License, you have\n\
627 permission to link or combine any covered work with a work licensed\n\
628 under version 3 of the GNU Affero General Public License into a single\n\
629 combined work, and to convey the resulting work.  The terms of this\n\
630 License will continue to apply to the part which is the covered work,\n\
631 but the special requirements of the GNU Affero General Public License,\n\
632 section 13, concerning interaction through a network will apply to the\n\
633 combination as such.\n\
634 \n\
635   14. Revised Versions of this License.\n\
636 \n\
637   The Free Software Foundation may publish revised and/or new versions of\n\
638 the GNU General Public License from time to time.  Such new versions will\n\
639 be similar in spirit to the present version, but may differ in detail to\n\
640 address new problems or concerns.\n\
641 \n\
642   Each version is given a distinguishing version number.  If the\n\
643 Program specifies that a certain numbered version of the GNU General\n\
644 Public License \"or any later version\" applies to it, you have the\n\
645 option of following the terms and conditions either of that numbered\n\
646 version or of any later version published by the Free Software\n\
647 Foundation.  If the Program does not specify a version number of the\n\
648 GNU General Public License, you may choose any version ever published\n\
649 by the Free Software Foundation.\n\
650 \n\
651   If the Program specifies that a proxy can decide which future\n\
652 versions of the GNU General Public License can be used, that proxy's\n\
653 public statement of acceptance of a version permanently authorizes you\n\
654 to choose that version for the Program.\n\
655 \n\
656   Later license versions may give you additional or different\n\
657 permissions.  However, no additional obligations are imposed on any\n\
658 author or copyright holder as a result of your choosing to follow a\n\
659 later version.\n\
660 ";
661 
662 const char program::warranty_t [] =
663 "  15. Disclaimer of Warranty.\n\
664 \n\
665   THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\n\
666 APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\n\
667 HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\n\
668 OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\n\
669 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n\
670 PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\n\
671 IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\n\
672 ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\
673 \n\
674   16. Limitation of Liability.\n\
675 \n\
676   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n\
677 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\n\
678 THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\n\
679 GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\n\
680 USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\n\
681 DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\n\
682 PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\n\
683 EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\n\
684 SUCH DAMAGES.\n\
685 \n\
686   17. Interpretation of Sections 15 and 16.\n\
687 \n\
688   If the disclaimer of warranty and limitation of liability provided\n\
689 above cannot be given local legal effect according to their terms,\n\
690 reviewing courts shall apply local law that most closely approximates\n\
691 an absolute waiver of all civil liability in connection with the\n\
692 Program, unless a warranty or assumption of liability accompanies a\n\
693 copy of the Program in return for a fee.\n\
694 ";
695 
simple_pager(const char * t)696 static void simple_pager (const char *t)
697 {
698   struct winsize w;
699   char c;
700 
701   if (!isatty (1)
702       || 0 > ioctl (1, TIOCGWINSZ, &w)
703       || w.ws_row < 2
704       || w.ws_col < 76) {
705     cout << t;
706     return;
707   }
708 
709   int lines = 0;
710   const char *start = t;
711   const char *end = t;
712   while ((end = strchr (end, '\n')) && *++end) {
713     if (++lines >= w.ws_row-1) {
714       cout << string (start, end-start);
715       lines = 0;
716       start = end;
717       cout << "Press enter to continue" << flush;
718       while (cin.get(c) && '\n' != c)
719 	;
720     }
721   }
722   if (*start)
723     cout << start; // Print the last page.
724 }
725 
warranty()726 void program::warranty ()
727 {
728   simple_pager (program::warranty_t);
729 }
730 
copying()731 void program::copying ()
732 {
733   simple_pager (program::copying_t);
734 }
735 
wrapif(int & column,int cols,int wid)736 static void wrapif (int& column, int cols, int wid)
737 {
738   if (cols < (column += wid)) {
739     cerr << endl;
740     column = wid;
741   }
742 }
743 
usage(struct option * long_opts,char * short_opts)744 void program::usage (struct option *long_opts, char *short_opts)
745 {
746   struct option *op;
747   string charops;
748   char *short_set = strdup (short_opts);
749   char *tmp_set;
750   struct winsize w;
751   int column;
752 
753   if (!isatty (1)
754       || 0 > ioctl (1, TIOCGWINSZ, &w)
755       || w.ws_col < 40)
756     w.ws_col = 80;
757 
758   cerr << "Usage: " << name;
759   column = 7 + strlen (name);
760   for (op = long_opts; op->name; op++) {
761     if (!op->flag && (tmp_set = strchr (short_set, op->val)))
762       *tmp_set = ' ';
763     switch (op->has_arg) {
764     case required_argument:
765       wrapif (column, w.ws_col, 5 + strlen (op->name) + 6);
766       cerr << " [ --" << op->name << "=arg ]";
767       if (!op->flag) {
768 	wrapif (column, w.ws_col, 11);
769 	cerr << " [ -" << (char)op->val << " arg ]";
770       }
771       break;
772     case optional_argument:
773       wrapif (column, w.ws_col, 5 + strlen (op->name) + 9);
774       cerr << " [ --" << op->name << "[=arg ] ]";
775       if (!op->flag) {
776 	wrapif (column, w.ws_col, 15);
777 	cerr << " [ -" << (char)op->val << " [ arg ] ]";
778       }
779       break;
780     default:
781       wrapif (column, w.ws_col, 5 + strlen (op->name) + 2);
782       cerr << " [ --" << op->name << " ]";
783       if (!op->flag)
784 	charops += op->val;
785     }
786   }
787   for (tmp_set = short_set; *tmp_set; tmp_set++) {
788     if (' ' != *tmp_set && ':' != *tmp_set) {
789       if (':' == tmp_set [1]) {
790 	wrapif (column, w.ws_col, 12);
791 	cerr << " [ -" << *tmp_set++ << " arg ]";
792       } else
793 	charops += *tmp_set;
794     }
795   }
796   if (charops.length ()) {
797     wrapif (column, w.ws_col, 4 + charops.length () + 2);
798     cerr << " [ -" << charops << " ]";
799   }
800   cerr << endl;
801   exit (EX_USAGE);
802 }
803 
804