diff options
author | Vladimir Azarov <avm@intermediate-node.net> | 2024-11-05 20:24:40 +0100 |
---|---|---|
committer | Vladimir Azarov <avm@intermediate-node.net> | 2024-11-05 21:49:55 +0100 |
commit | f777f6a9450d2bd5fc7ec531e6fb69f79942499a (patch) | |
tree | dc33cd941f49708274a74548862ba7b133013c55 | |
parent | 3972268b97b789a8212ea9bc7d1b9ce9905a68d8 (diff) |
Musl make
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | COPYRIGHT | 14 | ||||
-rw-r--r-- | LICENSE.GPL3 | 674 | ||||
-rw-r--r-- | Makefile | 32 | ||||
-rw-r--r-- | README | 43 | ||||
-rwxr-xr-x | compile_make.sh | 29 | ||||
-rw-r--r-- | tools/TODO | 4 | ||||
-rw-r--r-- | tools/common.c | 313 | ||||
-rw-r--r-- | tools/common.h | 125 | ||||
-rw-r--r-- | tools/make.c | 2455 | ||||
-rw-r--r-- | tools/sed.c | 18 |
11 files changed, 3612 insertions, 97 deletions
@@ -2,3 +2,5 @@ *.a generated tools/sed +tools/make +musl-*.tar.gz @@ -1,7 +1,17 @@ +All files in tools/ directory except "mkalltypes.sed" are available +under GPL3. See LICENSE.GPL3 file. + +Aforementioned files are copyrighted by + Vladimir Azarov avm@intermediate-node.net + +Everything else is available under its orginal license. See below. + +========================================================================== + musl as a whole is licensed under the following standard MIT license: ---------------------------------------------------------------------- -Copyright © 2005-2020 Rich Felker, et al. +Copyright (C) 2005-2020 Rich Felker, et al. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -112,7 +122,7 @@ Portions of this software are derived from third-party works licensed under terms compatible with the above MIT license: The TRE regular expression implementation (src/regex/reg* and -src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed +src/regex/tre*) is Copyright (C) 2001-2008 Ville Laurikari and licensed under a 2-clause BSD license (license text in the source files). The included version has been heavily modified by Rich Felker in 2012, in the interests of size, simplicity, and namespace cleanliness. diff --git a/LICENSE.GPL3 b/LICENSE.GPL3 new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE.GPL3 @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<https://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<https://www.gnu.org/licenses/why-not-lgpl.html>. @@ -1,6 +1,6 @@ AR = ar CC = gcc -CFLAGS = -O0 -g -Wall +CFLAGS = -g arch = x86_64 @@ -12,18 +12,18 @@ cflags = -pipe -std=c99 -nostdinc -D_XOPEN_SOURCE=700 \ -Iarch/$(arch) -Iarch/generic -Isrc/include -Isrc/internal \ -Igenerated/include -Iinclude $(CFLAGS) -prefix = / +prefix = /tmp/root includedir = $(prefix)/include libdir = $(prefix)/lib -src_dirs = src/* src/mallocng crt +src_dirs = crt src/* src/mallocng common_src = $(wildcard $(addsuffix /*.c, $(src_dirs))) arch_src = $(wildcard $(addsuffix /$(arch)/*.[csS], $(src_dirs))) common_obj = $(common_src:.c=.o) arch_obj = $(patsubst %, %.o, $(basename $(arch_src))) replaced_obj = $(subst /$(arch)/,/, $(arch_obj)) -all_obj = $(filter-out $(replaced_obj), $(common_obj) $(arch_obj)) +all_obj = $(sort $(filter-out $(replaced_obj), $(common_obj) $(arch_obj))) memops_obj = $(filter %/memcpy.o %/memmove.o %/memcmp.o %/memset.o, $(all_obj)) crt_obj = $(filter crt/%, $(all_obj)) @@ -50,10 +50,10 @@ gcc_prefix = $(dir $(shell gcc -print-libgcc-file-name)) tool_cflags = -Wall -nostdinc -Wno-main \ -ffreestanding -fno-pic -fno-stack-protector \ -Igenerated/include -Iarch/$(arch) \ - -I /usr/lib/clang/17/include \ + -I$(gcc_prefix)include \ -Iarch/generic $(CFLAGS) -.PRECIOUS: $(addprefix tools/, common.o sed.o) +.PRECIOUS: $(addprefix tools/, common.o sed.o make.o) tools/%.o: tools/%.c tools/common.h $(CC) $(tool_cflags) -c -o $@ $< @@ -68,18 +68,16 @@ default: $(lib_obj) $(dirs): mkdir -p $@ -$(generated_headers): | $(dirs) - generated/include/bits/alltypes.h: arch/$(arch)/bits/alltypes.h.in \ - include/alltypes.h.in tools/sed + include/alltypes.h.in tools/sed | $(dirs) cat $(filter %.in, $^) | tools/sed -f tools/mkalltypes.sed >$@ -generated/include/bits/syscall.h: arch/$(arch)/bits/syscall.h.in tools/sed +generated/include/bits/syscall.h: arch/$(arch)/bits/syscall.h.in \ + tools/sed | $(dirs) cp $< $@ tools/sed 's/__NR_/SYS_/' < $< >>$@ src/internal/version.o: cflags += -DVERSION=\"$(shell cat VERSION)\" -crt/crt1.o: arch/$(arch)/crt_arch.h $(memops_obj): cflags += -fno-tree-loop-distribute-patterns $(crt_obj): cflags += -DCRT @@ -96,8 +94,12 @@ install_cmd = install -D -m 644 $< $@ %.o: %.c $(generated_headers) $(implementation_headers) $(cc_cmd) +crt/crt1.o: crt/crt1.c arch/$(arch)/crt_arch.h $(generated_headers) $(implementation_headers) + $(cc_cmd) + %.o: crt/$(arch)/%.o cp $< $@ + %.o: crt/%.o cp $< $@ @@ -124,7 +126,11 @@ install-libs: $(addprefix $(DESTDIR)$(libdir)/, $(lib_obj)) install-headers: $(all_inc:include/%=$(DESTDIR)$(includedir)/%) install: install-libs install-headers +musl-1.2.5-mod.tar.gz: + git archive --format=tar.gz --prefix=musl-1.2.5-mod/ -o $@ HEAD + clean: rm -rf generated - rm -f crt/*.o src/*/*.o libc.a crt1.o crti.o crtn.o - rm -f tools/*.o tools/sed + rm -f crt/*.o crt/*/*.o src/*/*.o src/*/*/*.o libc.a crt*.o + rm -f tools/*.o tools/sed tools/make + rm -f musl-*.tar.gz @@ -0,0 +1,43 @@ +This is modified version of musl libc. +Library was patched by Vladimir Azarov (avm). +See COPYRIGHT for license. + +Contact: licvidozu@gmail.com + +Unlike the original this version does not require GNU make and sed +to be built. All you need is gcc/binutils pair, core utilities +(e.g. coreutils) and a shell (e.g. dash). + +The only supported architecture is x86_64 with LP64 abi. All dynamic +linking support is removed. + +To compile and install: + 1. Adjust prefix variable in Makefile + 2. CC=gcc LD=ld ./compile_make.sh + 3. tools/make + 4. tools/make install + +========================================================================== + + musl libc + +musl, pronounced like the word "mussel", is an MIT-licensed +implementation of the standard C library targetting the Linux syscall +API, suitable for use in a wide range of deployment environments. musl +offers efficient static and dynamic linking support, lightweight code +and low runtime overhead, strong fail-safe guarantees under correct +usage, and correctness in the sense of standards conformance and +safety. musl is built on the principle that these goals are best +achieved through simple code that is easy to understand and maintain. + +The 1.1 release series for musl features coverage for all interfaces +defined in ISO C99 and POSIX 2008 base, along with a number of +non-standardized interfaces for compatibility with Linux, BSD, and +glibc functionality. + +For basic installation instructions, see the included INSTALL file. +Information on full musl-targeted compiler toolchains, system +bootstrapping, and Linux distributions built on musl can be found on +the project website: + + http://www.musl-libc.org/ diff --git a/compile_make.sh b/compile_make.sh new file mode 100755 index 0000000..78bc6c0 --- /dev/null +++ b/compile_make.sh @@ -0,0 +1,29 @@ +#!/bin/sh -e + +defined() { + [ x"$2" = x ] && echo $1 is undefined && exit 1 + echo $1 = $2 +} + +pexec() { + echo $@ + $@ +} + +defined CC "$CC" +defined LD "$LD" + +arch=x86_64 + +gcc_prefix=$(dirname $(gcc -print-libgcc-file-name)) + +tool_cflags="-Wall -nostdinc -Wno-main \ + -ffreestanding -fno-pic -fno-stack-protector \ + -Igenerated/include -Iarch/$arch \ + -I${gcc_prefix}/include -Iarch/generic" + +cflags="$tool_cflags $CFLAGS" + +pexec $CC $cflags -c -o tools/common.o tools/common.c +pexec $CC $cflags -c -o tools/make.o tools/make.c +pexec $LD -nostdlib -o tools/make tools/make.o tools/common.o diff --git a/tools/TODO b/tools/TODO new file mode 100644 index 0000000..6f21b0c --- /dev/null +++ b/tools/TODO @@ -0,0 +1,4 @@ +* Remove coping: str2 for vtable search, str_n for variable values +* Cache variable values +* Cache rule execution status (file status) +* use execvp whenever possible diff --git a/tools/common.c b/tools/common.c index 00f7d3c..8156bec 100644 --- a/tools/common.c +++ b/tools/common.c @@ -50,6 +50,16 @@ int open(char *fname, int flags, int mode) flags|o_largefile, mode)); } +int chdir(char *path) +{ + return sysret(__syscall1(__NR_chdir, (long)path)); +} + +int getdents64(int fd, char *buf, int size) +{ + return sysret(__syscall3(__NR_getdents64, fd, (long)buf, size)); +} + int stat(char *fname, struct stat *st) { return sysret(__syscall2(__NR_stat, (long)fname, (long)st)); @@ -60,6 +70,21 @@ int fstat(int fd, struct stat *st) return sysret(__syscall2(__NR_fstat, (long)fd, (long)st)); } +int access(char *path, int mode) +{ + return sysret(__syscall2(__NR_access, (long)path, mode)); +} + +int dup2(int oldfd, int newfd) +{ + return sysret(__syscall2(__NR_dup2, oldfd, newfd)); +} + +int pipe(int *fds) +{ + return sysret(__syscall1(__NR_pipe, (long)fds)); +} + int close(int fd) { return sysret(__syscall1(__NR_close, fd)); @@ -69,7 +94,7 @@ void *mmap(char *addr, unsigned long length, int prot, int flags, int fd, unsigned long offset) { return (void*)sysret(__syscall6(__NR_mmap, (long)addr, length, - prot, flags, fd, offset)); + prot, flags, fd, offset)); } int munmap(char *addr, int length) @@ -79,11 +104,72 @@ int munmap(char *addr, int length) #define MREMAP_MAYMOVE 1 -int mremap(void *old_addr, unsigned long old_size, unsigned long new_size, +void *mremap(void *old_addr, unsigned long old_size, unsigned long new_size, int flags, void *new_addr) { - return sysret(__syscall5(__NR_mremap, (long)old_addr, old_size, - new_size, flags, (long)new_addr)); + return (void *)sysret(__syscall5(__NR_mremap, (long)old_addr, + old_size, new_size, flags, (long)new_addr)); +} + +int fork() +{ + return sysret(__syscall0(__NR_fork)); +} + +int wait4(int pid, int *wstatus, int options, void *rusage) +{ + return sysret(__syscall6(__NR_wait4, pid, (long)wstatus, options, + (long)rusage, 0, 0)); +} + +int execve(char *path, char **argv, char **envp) +{ + return sysret(__syscall3(__NR_execve, (long)path, (long)argv, + (long)envp)); +} + +int getrusage(int who, struct rusage *rusage) +{ + return sysret(__syscall2(__NR_getrusage, who, (long)rusage)); +} + +enum { + dirbuf_size = 16 * 1024 +}; + +int opendir(struct dir *dir, char *path) +{ + dir->fd = open(path, O_RDONLY|O_DIRECTORY, 0); + if (dir->fd == -1) + return 0; + + dir->buf = malloc(dirbuf_size); + dir->cur = dir->buf; + dir->end = dir->cur; + dir->path = path; + return 1; +} + +void closedir(struct dir *dir) +{ + close(dir->fd); +} + +struct dirent *readdir(struct dir *dir) +{ + struct dirent *tmp; + if (dir->cur == dir->end) { + int r = getdents64(dir->fd, dir->buf, dirbuf_size); + if (r == -1) + syscall_error(dir->path); + if (r == 0) + return NULL; + dir->cur = dir->buf; + dir->end = dir->buf + r; + } + tmp = (struct dirent *)dir->cur; + dir->cur += tmp->dsize; + return tmp; } int strzlen(char *s) @@ -94,6 +180,14 @@ int strzlen(char *s) return s - sc; } +struct str *debug2str(char *s) +{ + static struct str str; + str.start = s; + str.end = str.start + strzlen(s); + return &str; +} + int strzeq(char *s1, char *s2) { for (; *s1 && *s1 == *s2; ++s1, ++s2) @@ -104,15 +198,32 @@ int strzeq(char *s1, char *s2) int streq(struct str *s1, struct str *s2) { int i; + char * restrict tmp1, * restrict tmp2; if (s1->end - s1->start != s2->end - s2->start) return 0; + + tmp1 = s1->start; + tmp2 = s2->start; for (i = 0; s1->start + i < s1->end; ++i) { - if (s1->start[i] != s2->start[i]) + if (tmp1[i] != tmp2[i]) return 0; } return 1; } +int strge(struct str *s1, struct str *s2) +{ + char *tmp1 =s1->start, *tmp2 = s2->start; + for (;; ++tmp1, ++tmp2) { + if (tmp1 == s1->end) + return tmp2 == s2->end; + if (tmp2 == s2->end) + return 1; + if (*tmp1 != *tmp2) + return *tmp1 > *tmp2; + } +} + void memmove(char *dest, char *src, int len) { int i; @@ -125,11 +236,11 @@ void memmove(char *dest, char *src, int len) } } -void memcpy(void *dest, void *src, int len) +void memcpy(void * restrict dest, void * restrict src, int len) { - int i; char *d = dest; char *s = src; + int i; for (i = 0; i < len; ++i) d[i] = s[i]; } @@ -141,6 +252,18 @@ void memset(void *dest, char c, int len) *tmp = c; } +int memeq(void *m1, void *m2, int len) +{ + char *s1 = m1; + char *s2 = m2; + int i; + for (i = 0; i < len; ++i) { + if (s1[i] != s2[i]) + return 0; + } + return 1; +} + #define E(e, s) [e] = s, char *estrings[256] = { @@ -156,12 +279,6 @@ void perror(char *s) eprintf("%s: %s\n", s, estrings[idx]); } -void syscall_error(char *fname) -{ - perror(fname); - hexit(1); -} - enum { buflen = 32 * 1024 }; @@ -175,9 +292,12 @@ struct iobuf { struct iobuf stdout = { 1 }; struct iobuf stderr = { 2 }; -static void flush(struct iobuf *buf) +void flush(struct iobuf *buf) { - int ret = write(buf->fd, buf->buf, buf->len); + int ret; + if (buf->len == 0) + return; + ret = write(buf->fd, buf->buf, buf->len); if (ret != buf->len) { write(2, _S("some stdout/stderr error\n")); exit(3); @@ -185,6 +305,12 @@ static void flush(struct iobuf *buf) buf->len = 0; } +void syscall_error(char *fname) +{ + perror(fname); + hexit(1); +} + static void putchar(struct iobuf *buf, char c) { if (buf->len == buflen) @@ -204,7 +330,7 @@ void hexit(int code) } static void print_int(struct iobuf *buf, int nsigned, int byte4, - unsigned long n) + unsigned long n, int size) { char tmpbuf[64]; char *p = tmpbuf; @@ -238,6 +364,10 @@ static void print_int(struct iobuf *buf, int nsigned, int byte4, *p = k % 10 + '0'; ++p; } + if (p - tmpbuf < size) { + for (; p < tmpbuf + size; ++p) + *p = '0'; + } for (--p;; --p) { putchar(buf, *p); if (p == tmpbuf) @@ -258,30 +388,38 @@ void print_str(struct iobuf *buf, struct str *s) putchar(buf, *tmp); } -static void format(struct iobuf *buf, char c, va_list vl) +static void format(struct iobuf *buf, char c, int size, va_list vl) { switch(c) { unsigned long arg; case '%': + if (size != -1) + goto size_err; putchar(buf, '%'); break; case 'd': case 'u': arg = va_arg(vl, unsigned int); - print_int(buf, c == 'd', 1, arg); + print_int(buf, c == 'd', 1, arg, size); break; case 'D': case 'U': arg = va_arg(vl, unsigned long); - print_int(buf, c == 'D', 0, arg); + print_int(buf, c == 'D', 0, arg, size); break; case 's': + if (size != -1) + goto size_err; print_strz(buf, va_arg(vl, char*)); break; case 'S': + if (size != -1) + goto size_err; print_str(buf, va_arg(vl, struct str *)); break; case 'c': + if (size != -1) + goto size_err; arg = va_arg(vl, int); putchar(buf, arg); break; @@ -289,23 +427,35 @@ static void format(struct iobuf *buf, char c, va_list vl) write(2, _S("printf: unexpected format option \n")); hexit(2); } + return; +size_err: + write(2, _S("printf: %c: size not applicable\n")); + hexit(2); } static void __printf(struct iobuf *buf, char *s, va_list vl) { for (; *s; ++s) { if (*s == '%') { - if (s[1] == 0) { - write(2, _S("printf: nothing comes after '%'")); - hexit(2); + int size = -1; + if (s[1] == 0) + goto zero_err; + if (s[1] >= '0' && s[1] <= '9') { + size = s[1] - '0'; + ++s; } - format(buf, s[1], vl); + if (s[1] == 0) + goto zero_err; + format(buf, s[1], size, vl); ++s; } else { putchar(buf, *s); } } return; +zero_err: + write(2, _S("printf: nothing specifier after '%'")); + hexit(2); } void printf(char *fmt, ...) @@ -318,8 +468,6 @@ void printf(char *fmt, ...) void evprintf(char *fmt, va_list vl) { - print_strz(&stderr, progname); - print_strz(&stderr, ": "); __printf(&stderr, fmt, vl); } @@ -331,38 +479,56 @@ void eprintf(char *fmt, ...) va_end(vl); } -/* Memory management */ +void error(char *fmt, ...) +{ + va_list vl; + va_start(vl, fmt); -struct mem_buffer { - char *cur, *end; - char *saved; -}; + print_strz(&stderr, progname); + print_strz(&stderr, ": "); + evprintf(fmt, vl); + + va_end(vl); + print_strz(&stderr, "\n"); + hexit(1); +} + +/* Memory management */ -static struct mem_buffer mem_buf; +struct heap heap; -static void mem_buffer_init() +void heap_offset_save() { - mem_buf.cur = mem_buf.end = brk(NULL); - mem_buf.saved = NULL; + if (heap.snum == heap_saves_max) + error("heap: offset stack is full"); + heap.saved[heap.snum] = heap.a.cur; + ++heap.snum; } -void save_heap_offset() +void heap_offset_remove() { - if (mem_buf.saved != NULL) { - eprintf("mem_buffer: offset already saved\n"); - hexit(2); + if (heap.snum == 0) { + error("heap: offset stack is empty"); } - mem_buf.saved = mem_buf.cur; + --heap.snum; } -void restore_heap_offset() +void heap_offset_restore() { - if (mem_buf.saved == NULL) { - eprintf("mem_buffer: no offset was saved\n"); - hexit(2); - } - mem_buf.cur = mem_buf.saved; - mem_buf.saved = NULL; + if (heap.snum == 0) + error("mem_buffer: no offset was saved\n"); + heap.a.cur = heap.saved[heap.snum - 1]; + --heap.snum; + heap.last_alloc = NULL; +} + +void *mem_alloc(int size) +{ + void *tmp = mmap(NULL, size, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANON, -1, 0); + if (tmp == (void*)-1) + syscall_error("mmap"); + return tmp; } static int get_alignment(int n) @@ -378,7 +544,7 @@ static int get_alignment(int n) return 1; } -static void mem_buf_grow(int size) +static void heap_grow(struct arena *a, int size) { char *tmp; if (size < 128*1024) { @@ -388,31 +554,58 @@ static void mem_buf_grow(int size) size &= -4096; } /*printf("Growing (%d)\n", size);*/ - tmp = brk(mem_buf.end + size); - if (tmp == mem_buf.end) { - eprintf("mem_buf: unable to allocate memory\n"); - hexit(1); - } - mem_buf.end = tmp; + tmp = brk(a->end + size); + if (tmp == a->end) + error("heap: unable to grow"); + a->end = tmp; +} + +static void heap_init() +{ + heap.a.start = heap.a.cur = heap.a.end = brk(NULL); + heap.a.grow = heap_grow; + + heap.snum = 0; + heap.last_alloc = NULL; } #define ALIGN(v, align) \ (void*)(((unsigned long)(v) + (align) - 1ul) & -(align)) -void *malloc(int size) +void *amalloc(struct arena *a, int size) { int align = get_alignment(size); - char *tmp = ALIGN(mem_buf.cur, align); - int diff = tmp + size - mem_buf.end; + char *tmp = ALIGN(a->cur, align); + int diff = tmp + size - a->end; if (diff > 0) - mem_buf_grow(diff); + a->grow(a, diff); + + a->cur = tmp + size; + + return tmp; +} - mem_buf.cur = tmp + size; +void *malloc(int size) +{ + void *tmp = amalloc(&heap.a, size); + heap.last_alloc = tmp; return tmp; } -int main(int argc, char **argv, char **envp); +void realloc(void *p, int newsize) +{ + int diff; + if (heap.last_alloc != p) + error("realloc: not a previously allocated region"); + diff = heap.last_alloc + newsize - heap.a.end; + if (diff > 0) + heap.a.grow(&heap.a, diff); + + heap.a.cur = heap.last_alloc + newsize; +} + +extern int main(int argc, char **argv, char **envp); void _start_c(unsigned long *p) { @@ -421,7 +614,7 @@ void _start_c(unsigned long *p) char **envp = argv + argc + 1; progname = argv[0]; - mem_buffer_init(); + heap_init(); hexit(main(argc, argv, envp)); } diff --git a/tools/common.h b/tools/common.h index 43e2dda..4ca27bd 100644 --- a/tools/common.h +++ b/tools/common.h @@ -5,24 +5,34 @@ #include <stdarg.h> +extern int main(int argc, char **argv, char **envp); + #define NULL ((void *)0) -#define _S(s) s, (sizeof(s) - 1) -#define CSTR(s) ((struct str) { s, s + sizeof(s) - 1 }) -#define STR(s) ((struct str) { s, s + strzlen(s) }) +#define _S(s) (s), (sizeof(s) - 1) +#define CSTR(s) ((struct str) { (s), (s) + sizeof(s) - 1 }) +#define STR(s) ((struct str) { (s), (s) + strzlen(s) }) + +struct str *debug2str(char *s); struct str { char *start; char *end; }; +enum { + heap_saves_max = 16 +}; + extern int errno; extern char *progname; void exit(int code); +int chdir(char *path); int read(int fd, char *buf, int len); int write(int fd, char *buf, int len); #define O_RDONLY 0 +#define O_DIRECTORY 0200000 int open(char *fname, int flags, int mode); @@ -60,6 +70,34 @@ struct stat { int stat(char *fname, struct stat *st); int fstat(int fd, struct stat *st); +struct dir { + int fd; + + char *buf; + char *cur; + char *end; + + char *path; +}; + +struct dirent { + unsigned long inode; + unsigned long offset; + unsigned short dsize; + unsigned char type; + char name[1]; +}; + +int opendir(struct dir *dir, char *path); +struct dirent *readdir(struct dir *dir); +void closedir(struct dir *dir); + +#define F_OK 0 + +int access(char *path, int mode); + +int dup2(int oldfd, int newfd); +int pipe(int *fds); int close(int fd); #define MAP_PRIVATE 0x02 @@ -76,8 +114,46 @@ int munmap(char *addr, int length); #define MREMAP_MAYMOVE 1 -int mremap(void *old_addr, unsigned long old_size, unsigned long new_size, - int flags, void *new_addr); +void *mremap(void *old_addr, unsigned long old_size, + unsigned long new_size, int flags, void *new_addr); + +int fork(); + +#define WIFEXITED(s) (!WTERMSIG(s)) +#define WEXITSTATUS(s) (((s) >> 8) & 0xff) +#define WIFSIGNALED(s) (((s) & 0xffff) -1 < 0xff) +#define WTERMSIG(s) ((s) & 0x7f) + +int wait4(int pid, int *wstatus, int options, void *rusage); +int execve(char *path, char **argv, char **envp); + +struct timeval { + unsigned long long sec; + unsigned long long msec; +}; + +struct rusage { + struct timeval utime; + struct timeval stime; + long maxrss; + long ixrss; + long idrss; + long isrss; + long minflt; + long majflt; + long nswap; + long inblock; + long oublock; + long msgsnd; + long msgrcv; + long nsignals; + long nvcsw; + long nivcsw; +}; + +#define RUSAGE_SELF 0 + +int getrusage(int who, struct rusage *rusage); void hexit(int code); @@ -90,19 +166,52 @@ void print_str(struct iobuf *buf, struct str *s); void printf(char *fmt, ...); void eprintf(char *fmt, ...); void evprintf(char *fmt, va_list vl); +void error(char *fmt, ...); + +struct arena; + +typedef void arena_grow_func(struct arena *a, int size); + +struct arena { + char *start; + char *cur; + char *end; -void save_heap_offset(); -void restore_heap_offset(); + arena_grow_func *grow; +}; + +struct heap { + struct arena a; + + char *saved[heap_saves_max]; + int snum; + + char *last_alloc; +}; + +extern struct heap heap; + +void heap_offset_save(); +void heap_offset_remove(); +void heap_offset_restore(); + +void *mem_alloc(int size); +void *amalloc(struct arena *a, int size); void *malloc(int size); +void realloc(void *p, int newsize); int strzlen(char *s); int strzeq(char *s1, char *s2); int streq(struct str *s1, struct str *s2); +int strge(struct str *s1, struct str *s2); +void flush(struct iobuf *buf); void perror(char *s); void syscall_error(char *fname); void memmove(char *dest, char *src, int len); -void memcpy(void *dest, void *src, int len); +void memcpy(void *restrict dest, void *restrict src, int len); void memset(void *dest, char c, int len); +int memeq(void *m1, void *m2, int len); + #endif diff --git a/tools/make.c b/tools/make.c new file mode 100644 index 0000000..0b1f704 --- /dev/null +++ b/tools/make.c @@ -0,0 +1,2455 @@ +#include "common.h" + +enum { + path_max_len = 4096, + buf_size = 1024 * 1024, + buf_static_size = 512, + hash_table_size = 16 * 1024, + pat_rules_limit = 16, + find_body_max_depth = 2 +}; + +const char *debug_prx = ">>> "; + +struct conf { + char *target; + char *makefile; + int debug_mode; +}; + +struct line { + int linenum; + struct str str; +}; + +struct buf { + char *start; + char *end; + char *cur; +}; + +struct augstr { + struct str str; + int auxval; +}; + +struct strbuf { + struct augstr *buf; + int len; + int size; +}; + +struct rule_body { + struct strbuf prereqs; + int normal_prereq_count; + + struct strbuf *recipes; +}; + +struct full_rule { + struct str *target; + struct rule_body *rest; +}; + +struct rule_details { + struct rule_body *body; + struct strbuf targets; + int patrule; + int lineno; +}; + +struct vt_cell { + struct str vname; + struct str value; +}; + +struct vtable { + struct vt_cell *buf; + int cells_occupied; +}; + +struct rule_cell { + struct str *target; + struct rule_body *body; +}; + +struct rtable { + struct rule_cell *buf; + int cells_occupied; +}; + +struct world { + char *makefile; + struct str target; + char **envp; + + int debug_mode; + + struct full_rule pat_rules[pat_rules_limit]; + int pat_rule_num; + + struct vtable vtable; + struct rtable rtable; +}; + +struct eval_aux { + int level; + int linenum; + struct full_rule *rule; + + struct buf *mainbuf; + struct buf *auxbuf; + struct world *world; +}; + +static void lerror(int line, char *str, ...) +{ + va_list vl; + + eprintf("%s: line %d: ", progname, line); + + va_start(vl, str); + evprintf(str, vl); + + print_strz(&stderr, "\n"); + va_end(vl); + exit(1); +} + +static void strdup(struct str *dest, struct str *src, int with_zero) +{ + int slen = src->end - src->start; + int len = slen + (with_zero ? 1 : 0); + dest->start = malloc(len); + dest->end = dest->start + slen; + + memcpy(dest->start, src->start, slen); + if (with_zero) + dest->start[slen] = 0; +} + +#define HASHTABLE_INIT(type) \ +static void type ## _init(struct type *t) \ +{ \ + t->buf = malloc(hash_table_size * sizeof(*t->buf)); \ + t->cells_occupied = 0; \ + memset(t->buf, 0, hash_table_size * sizeof(*t->buf)); \ +} + +HASHTABLE_INIT(vtable) +HASHTABLE_INIT(rtable) + +/* FNV-1a */ +static unsigned int calc_hash(struct str *str) +{ + char *tmp; + unsigned int h = 2166136261; + for (tmp = str->start; tmp != str->end; ++tmp) { + h = h ^ *tmp; + h *= 16777619; + } + return h; +} + +static struct rule_cell *rtable_getcell(struct rtable *t, struct str *key) +{ + unsigned hash = calc_hash(key); + int i = hash & (hash_table_size - 1); + for (;;) { + if (t->buf[i].target == NULL) + break; + else if (streq(key, t->buf[i].target)) + break; + i = (i + 1) & (hash_table_size - 1); + } + return t->buf + i; +} + +static struct vt_cell *vtable_getcell(struct vtable *t, struct str *key) +{ + unsigned hash = calc_hash(key); + int i = hash & (hash_table_size - 1); + for (;;) { + if (t->buf[i].vname.start == NULL) + break; + else if (streq(key, &t->buf[i].vname)) + break; + i = (i + 1) & (hash_table_size - 1); + } + return t->buf + i; +} + +static void vtable_update(struct vtable *vt, struct str *key, + int alloc_name, struct str *value) +{ + struct vt_cell *cell = vtable_getcell(vt, key); + if (cell->vname.start == NULL) { + ++vt->cells_occupied; + if ((double)vt->cells_occupied / hash_table_size >= 0.7) + error("vtable: threshold reached"); + } + if (cell->vname.start == NULL) { + if (alloc_name) + strdup(&cell->vname, key, 0); + else + cell->vname = *key; + } + cell->value = *value; +} + +static struct str *vtable_find(struct vtable *vt, struct str *key) +{ + struct vt_cell *tmp = vtable_getcell(vt, key); + if (tmp->vname.start == NULL) + return NULL; + return &tmp->value; +} + +static void rtable_add(struct rtable *rt, struct str *target, + int alloc_name, struct rule_body *rule) +{ + struct rule_cell *cell = rtable_getcell(rt, target); + if (cell->target != NULL) + error("%S: rule redefinition", target); + if ((double)rt->cells_occupied / hash_table_size >= 0.7) + error("rtable: threshold reached"); + if (alloc_name) { + cell->target = malloc(sizeof(*cell->target)); + strdup(cell->target, target, 0); + } else { + cell->target = target; + } + cell->body = rule; + ++rt->cells_occupied; +} + +static int rtable_find(struct rtable *rt, struct str *key, + struct rule_body **val) +{ + struct rule_cell *cell = rtable_getcell(rt, key); + if (cell->target == NULL) + return 0; + *val = cell->body; + return 1; +} + +static void parse_cmdargs(struct conf *conf, char **argv) +{ + ++argv; + for (; argv[0]; ++argv) { + if (argv[0][0] == '-') { + if (argv[0][1] == 'f' && argv[0][2] == 0) { + if (argv[1] == NULL) + error("-f: missing file name"); + conf->makefile = argv[1]; + ++argv; + } else if (argv[0][1] == 'C' && argv[0][2] == 0) { + if (argv[1] == NULL) + error("-C: missing dir"); + if (chdir(argv[1]) == -1) + syscall_error(argv[1]); + ++argv; + } else if (argv[0][1] == 'd' && argv[0][2] == 0) { + conf->debug_mode = 1; + } else { + error("%s: unknown flag", argv[0]); + } + } else { + if (conf->target) + error("%s: target already provided"); + conf->target = argv[0]; + } + } + if (conf->makefile == NULL) + conf->makefile = "Makefile"; +} + +static char *mmap_makefile(char *fname) +{ + int fd = open(fname, O_RDONLY, 0); + struct stat st; + char *tmp; + + if (fd == -1) + goto syserr; + if (fstat(fd, &st) == -1) + goto syserr; + + tmp = mmap(NULL, st.size + 1, PROT_READ|PROT_WRITE, + MAP_PRIVATE, fd, 0); + if (tmp == (void*)-1) + goto syserr; + if (close(fd) == -1) + goto syserr; + return tmp; +syserr: + syscall_error(fname); + return NULL; /* unreachable */ +} + +static int readline(char **makefile, struct line *line) +{ + static int linenum = 1; + char *tmp = *makefile; + char *marker = NULL; + + for (; *tmp && *tmp != '\n'; ++tmp) { + if (*tmp == '#' && marker == NULL) + marker = tmp; + else if (tmp[0] == '\\' && tmp[1] == '\n') { + tmp[0] = tmp[1] = ' '; + ++tmp; + ++linenum; + } + } + + if (*tmp == 0) { + if (tmp == *makefile) + return 0; + else + error("last line missing '\\n'"); + } + + *tmp = 0; + line->linenum = linenum; + ++linenum; + line->str.start = *makefile; + line->str.end = marker ? marker : tmp; + *makefile = tmp + 1; + + return 1; +} + +static int nextline_starts_with_tab(char *file) +{ + return file[0] == '\t'; +} + +#define buf_static_init(buf, local_buf, size) \ +do { \ + buf.start = local_buf; \ + buf.end = buf.start + size; \ + buf.cur = buf.start; \ +} while (0) + +static void buf_init(struct buf *buf) +{ + buf->start = mem_alloc(buf_size); + buf->end = buf->start + buf_size; + buf->cur = buf->start; +} + +static void __buf_putchar(struct buf *buf, char c, int inc) +{ + if (buf->cur == buf->end) + error("buf_putchar: buffer resizing is not supported"); + if (c == '\t') + c = ' '; + if (c == ' ' && buf->cur > buf->start && buf->cur[-1] == ' ') + return; + *buf->cur = c; + if (inc) + ++buf->cur; +} + +static void buf_putchar(struct buf *buf, char c) +{ + __buf_putchar(buf, c, 1); +} + +static void buf_put_silent_zero(struct buf *buf) +{ + __buf_putchar(buf, 0, 0); +} + +static void buf_puts(struct buf *buf, struct str *str) +{ + char *tmp; + for (tmp = str->start; tmp != str->end; ++tmp) + buf_putchar(buf, *tmp); +} + +static void buf_putsz(struct buf *buf, char *s) +{ + for (; *s; ++s) + buf_putchar(buf, *s); +} + +static int strempty(struct str *str) +{ + return str->start == str->end; +} + +static int strlen(struct str *str) +{ + return str->end - str->start; +} + +static char *find_cparent(int line, struct str *str) +{ + char *tmp; + int level = 0; + for (tmp = str->start + 1; tmp != str->end; ++tmp) { + if (*tmp == '(') + ++level; + if (*tmp == ')') { + if (level == 0) + return tmp; + else + --level; + } + } + lerror(line, "missing ')'"); + return NULL; /* unreachable */ +} + +enum { + id_variable, + id_subref, + + id_prereqs, + id_first_prereq, + id_target, + + id_func_class_start, + id_func_wildcard, + id_func_subst, + id_func_patsubst, + id_func_filter, + id_func_filter_out, + + id_func_basename, + id_func_dir, + + id_func_shell, + id_func_addprefix, + id_func_addsuffix, + id_func_sort, + id_func_class_end +}; + +struct eprofile { + char type; + struct str *body; + + struct str from; + struct str to; +}; + +static int is_space(char c) +{ + return c == ' ' || c == '\t'; +} + +static int valid_var_char(char c) +{ + return (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + ( c >= '0' && c <= '9') || + c == '_' || c == '/' || + c == '-' || c == '.'; +} + +static char *word_end(struct str *str) +{ + char *tmp; + for (tmp = str->start; tmp != str->end; ++tmp) { + if (is_space(*tmp)) + return tmp; + if (!valid_var_char(*tmp)) + return *tmp == ':' ? tmp : NULL; + } + return tmp; +} + +#define ENTRY(fname) { CSTR(#fname), id_func_ ## fname } + +static struct { struct str fname; char type; } func_table[] = { + ENTRY(wildcard), + ENTRY(subst), + ENTRY(patsubst), + ENTRY(filter), + { CSTR("filter-out"), id_func_filter_out }, + ENTRY(basename), + ENTRY(dir), + ENTRY(sort), + ENTRY(shell), + ENTRY(addprefix), + ENTRY(addsuffix) +}; +#undef ENTRY + +static int is_special_var(struct eprofile *prof, struct str *word) +{ + if (streq(word, &CSTR("^"))) { + prof->type = id_prereqs; + return 1; + } else if (streq(word, &CSTR("<"))) { + prof->type = id_first_prereq; + return 1; + } else if (streq(word, &CSTR("@"))) { + prof->type = id_target; + return 1; + } + return 0; +} + +static void check_var_form(struct str *word, struct str *all, + int line) +{ + if (word->end == all->end) + return; + lerror(line, "(%S): variable name must be a single word", all); +} + +static void parse_subref(struct eprofile *prof, struct str *vname, + struct str *all, int line) +{ + char *tmp; + prof->type = id_subref; + + all->start = vname->end + 1; + prof->from.start = all->start; + for (tmp = all->start; tmp != all->end && *tmp != '='; ++tmp) + ; + if (tmp == all->end) + lerror(line, "%s: no '=' is subref expr", vname); + prof->from.end = tmp; + prof->to.start = tmp + 1; + prof->to.end = all->end; + + *all = *vname; + prof->body = all; +} + +static void parse_profile(struct eprofile *prof, struct str *str, int line, + struct full_rule *rule) +{ + int i; + char *wend; + struct str first_word; + + if (rule != NULL && is_special_var(prof, str)) + return; + + wend = word_end(str); + if (wend == NULL) + lerror(line, "invalid symbol during first word parsing"); + first_word = (struct str) { str->start, wend }; + + if (wend != str->end && *wend == ':') { + parse_subref(prof, &first_word, str, line); + return; + } + + for (i = 0; i < sizeof(func_table) / sizeof(func_table[0]); ++i) { + if (streq(&first_word, &func_table[i].fname)) { + prof->type = func_table[i].type; + str->start = first_word.end; + prof->body = str; + return; + } + } + prof->type = id_variable; + check_var_form(&first_word, str, line); + prof->body = str; +} + +static void __eval(struct eval_aux *aux, struct str *str); + +static void swap_buffers(struct eval_aux *aux) +{ + struct buf *tmp = aux->mainbuf; + aux->mainbuf = aux->auxbuf; + aux->auxbuf = tmp; +} + +static void eval_wrapper(struct eval_aux *aux, struct str *str, + int swap, int zero_terminate) +{ + struct str tmp = *str; + ++aux->level; + if (swap) + swap_buffers(aux); + __eval(aux, &tmp); + if (zero_terminate) + buf_put_silent_zero(aux->mainbuf); + if (swap) + swap_buffers(aux); + --aux->level; +} + +static int adjust_vname(struct buf *buf, struct full_rule *rule, + struct str *vname) +{ + if (rule == NULL) + return 0; + buf_puts(buf, rule->target); + buf_putchar(buf, '#'); + buf_puts(buf, vname); + return 1; +} + +static void expand_variable(struct eval_aux *aux, struct eprofile *prof) +{ + char localbuf[buf_static_size]; + struct buf buf; + struct str *varval = NULL; + + buf_static_init(buf, localbuf, buf_static_size); + if (adjust_vname(&buf, aux->rule, prof->body)) { + struct str name = (struct str) { buf.start, buf.cur }; + varval = vtable_find(&aux->world->vtable, &name); + } + + if (varval == NULL) + varval = vtable_find(&aux->world->vtable, prof->body); + + if (varval == NULL) + return; + eval_wrapper(aux, varval, 0, 0); +} + +static void expand_first_prereq(struct eval_aux *aux, + struct eprofile *prof) +{ + if (aux->rule->rest->prereqs.len == 0) + return; + buf_puts(aux->mainbuf, &aux->rule->rest->prereqs.buf[0].str); +} + +static void expand_prereqs(struct eval_aux *aux, + struct eprofile *prof) +{ + int i; + struct strbuf *prereqs = &aux->rule->rest->prereqs; + for (i = 0; i < prereqs->len; ++i) { + buf_puts(aux->mainbuf, &prereqs->buf[i].str); + if (i + 1 != prereqs->len) + buf_putchar(aux->mainbuf, ' '); + } +} + +static void trim_front(struct str *str); +static void trim_back(struct str *str); +static void trim(struct str *str); + +static int extract_word(struct str *str, struct str *word) +{ + char *tmp; + + trim_front(str); + if (strempty(str)) + return 0; + + tmp = str->start; + word->start = tmp; + for (; tmp != str->end && !is_space(*tmp); ++tmp) + ; + word->end = tmp; + + str->start = tmp; + return 1; +} + +struct matched { + int front_remains; + int have_wildcard; + struct str wildcard; +}; + +static char *contains_char(struct str *str, char c); + +static int matches_wildcard(struct matched *m, struct str *pat, + struct str *str, char *wildcard) +{ + int ret; + int diff = wildcard - pat->start; + ret = memeq(str->start, pat->start, diff); + if (ret == 0) + return 0; + if (m) + m->wildcard.start = str->start + diff; + diff = pat->end - (wildcard + 1); + ret = memeq(str->end - diff, pat->end - diff, diff); + if (ret == 0) + return 0; + if (m) { + m->wildcard.end = str->end - diff; + m->have_wildcard = 1; + } + return 1; +} + +static int match_str(struct str *pat, struct str *str, + struct matched *m) +{ + char *tmp; + + if (m) { + m->front_remains = 0; + m->have_wildcard = 0; + } + + tmp = contains_char(pat, '%'); + if (tmp != NULL) { + if(!matches_wildcard(m, pat, str, tmp)) + return 0; + } else { + int ret; + int diff = strlen(str) - strlen(pat); + if (diff < 0) + return 0; + ret = memeq(str->start + diff, pat->start, strlen(pat)); + if (ret == 0) + return 0; + if (m) + m->front_remains = diff; + } + return 1; +} + +static int pmatches(struct eprofile *prof, struct str *value, + struct matched *m) +{ + return match_str(&prof->from, value, m); +} + +static void modify(struct eval_aux *aux, struct eprofile *prof, + struct str *value, struct matched *m) +{ + int i; + char *tmp; + struct str *template = &prof->to; + for (i = 0; i < m->front_remains; ++i) + buf_putchar(aux->mainbuf, value->start[i]); + for (tmp = template->start; tmp != template->end; ++tmp) { + if (*tmp == '%' && m->have_wildcard) + buf_puts(aux->mainbuf, &m->wildcard); + else + buf_putchar(aux->mainbuf, *tmp); + } +} + +static void subref(struct eval_aux *aux, struct eprofile *prof, + struct str *value) +{ + int first = 1; + for (;; first = 0) { + struct str word; + struct matched matched; + + if (!extract_word(value, &word)) + break; + + if (!first) + buf_putchar(aux->mainbuf, ' '); + + if (pmatches(prof, &word, &matched)) + modify(aux, prof, &word, &matched); + else + buf_puts(aux->mainbuf, &word); + } +} + +static void expand_subref(struct eval_aux *aux, struct eprofile *prof) +{ + char *savedpos = aux->auxbuf->cur; + char *tmp; + struct str value; + prof->type = id_variable; + + swap_buffers(aux); + expand_variable(aux, prof); + swap_buffers(aux); + value = (struct str) { savedpos, aux->auxbuf->cur }; + + tmp = aux->auxbuf->cur; + eval_wrapper(aux, &prof->from, 1, 0); + prof->from = (struct str) { tmp, aux->auxbuf->cur }; + + tmp = aux->auxbuf->cur; + eval_wrapper(aux, &prof->to, 1, 0); + prof->to = (struct str) { tmp, aux->auxbuf->cur }; + + subref(aux, prof, &value); + + aux->auxbuf->cur = savedpos; +} + +static void expand_target(struct eval_aux *aux) +{ + eval_wrapper(aux, aux->rule->target, 0, 0); +} + +static int extract_farg(struct str *src, struct str *arg) +{ + int level = 0; + char *tmp; + + arg->start = src->start; + for (tmp = src->start; tmp != src->end; ++tmp) { + if (*tmp == '(') + ++level; + else if (*tmp == ')') + --level; + else if (*tmp == ',' && level == 0) + break; + + } + arg->end = tmp; + if (tmp == src->end) + return 0; + src->start = tmp + 1; + return 1; +} + +static void basename_word(struct str *res, struct str *word) +{ + char *tmp = word->end - 1; + char *dot = NULL; + for (; tmp >= word->start; --tmp) { + if (*tmp == '.' && dot == NULL) { + dot = tmp; + break; + } + if (*tmp == '/') + break; + } + *res = (struct str) { + word->start, + dot ? dot : word->end + }; +} + +static void dir_word(struct str *res, struct str *word) +{ + char *tmp = word->end - 1; + char *slash = NULL; + + for (; tmp >= word->start; --tmp) { + if (*tmp == '/') { + slash = tmp; + break; + } + } + if (slash == NULL) + *res = CSTR("./"); + else + *res = (struct str) { word->start, slash + 1 }; +} + +static void extract_args(struct str *args, struct str *str, int argnum, + char *func, int linenum) +{ + int i; + for (i = 0;; ++i) { + int ret = extract_farg(str, args + i); + if (i == argnum - 1) { + if (ret == 1) + goto argcount_err; + else { + trim(args + i); + return; + } + } + if (ret == 0) + goto argcount_err; + trim(args + i); + } +argcount_err: + lerror(linenum, "%s expects %d argument%s", func, argnum, + argnum > 1 ? "s" : ""); +} + +enum { + mode_basename, + mode_dir, +}; + +static void expand_basename_or_dir(struct eval_aux *aux, int mode, + struct eprofile *prof) +{ + struct str arg; + int first = 1; + + char *func = mode == mode_basename ? "basename" : "dir"; + extract_args(&arg, prof->body, 1, func, aux->linenum); + + for (;; first = 0) { + struct str word, res; + + if(!extract_word(&arg, &word)) + break; + + if (mode == mode_basename) + basename_word(&res, &word); + else + dir_word(&res, &word); + if (!first) + buf_putchar(aux->mainbuf, ' '); + buf_puts(aux->mainbuf, &res); + } +} + +static int arg_is_single_word(struct str *str) +{ + struct str word; + + if (!extract_word(str, &word)) + return 0; + trim_front(str); + + if (!strempty(str)) + return 0; + *str = word; + return 1; +} + +enum { + mode_prefix, + mode_suffix +}; + +static void expand_add(struct eval_aux *aux, int mode, + struct eprofile *prof) +{ + struct str args[2]; + int first = 1; + + char *func = mode == mode_prefix ? "addprefix" : "addsuffix"; + extract_args(args, prof->body, 2, func, aux->linenum); + + if (!arg_is_single_word(args)) + lerror(aux->linenum, "%s: accepts a single prefix", func); + + for (;; first = 0) { + struct str word; + + if (!extract_word(args + 1, &word)) + break; + + if (!first) + buf_putchar(aux->mainbuf, ' '); + if (mode == mode_prefix) { + buf_puts(aux->mainbuf, args); + buf_puts(aux->mainbuf, &word); + } else { + buf_puts(aux->mainbuf, &word); + buf_puts(aux->mainbuf, args); + } + } + return; +} + +enum { + mode_filter, + mode_filter_out +}; + +static int passes_filter(struct str *str, struct strbuf *pats, int mode) +{ + int ret_when_matches = mode == mode_filter ? 1 : 0; + int i; + for (i = 0; i < pats->len; ++i) { + if (match_str(&pats->buf[i].str, str, NULL)) + return ret_when_matches; + } + return !ret_when_matches; +} + +static void sbuf_init(struct strbuf *buf); +static void sbuf_save(struct strbuf *buf, struct str *str, int aux); + +static void collect_words(struct strbuf *buf, struct str *words) +{ + sbuf_init(buf); + + for (;;) { + struct str word; + int ret = extract_word(words, &word); + if (ret == 0) + break; + sbuf_save(buf, &word, -1); + } +} + +static void filter_words(struct str *words, struct strbuf *pats, + struct eval_aux *aux, int mode) +{ + int first = 1; + for (;;) { + struct str word; + int ret = extract_word(words, &word); + if (ret == 0) + break; + if (!passes_filter(&word, pats, mode)) + continue; + if (!first) + buf_putchar(aux->mainbuf, ' '); + buf_puts(aux->mainbuf, &word); + first = 0; + } +} + +static void expand_filter(struct eval_aux *aux, int mode, + struct eprofile *prof) +{ + struct str args[2]; + struct strbuf pats; + + char *func = mode == mode_filter ? "filter" : "filter-out"; + extract_args(args, prof->body, 2, func, aux->linenum); + + heap_offset_save(); + collect_words(&pats, args); + filter_words(args + 1, &pats, aux, mode); + heap_offset_restore(); +} + +static void merge(struct augstr *to, struct augstr *from, int n) +{ + int mid = n/2; + int i, j, k; + + for (k = 0, i = 0, j = mid; i < mid && j < n; ++k) { + if (strge(&from[j].str, &from[i].str)) { + to[k].str = from[i].str; + ++i; + } else { + to[k].str = from[j].str; + ++j; + } + } + if (i < mid) { + for (; i < mid; ++i, ++k) + to[k].str = from[i].str; + } else { + for (; j < n; ++j, ++k) + to[k].str = from[j].str; + } +} + +static void mergesort(struct augstr *array, struct augstr *tmp, + int n) +{ + if (n < 2) + return; + mergesort(tmp, array, n/2); + mergesort(tmp + n/2, array + n/2, n - n/2); + merge(array, tmp, n); +} + +static void sort(struct strbuf *words) +{ + struct augstr *tmp = malloc(sizeof(*tmp) * words->len); + int i; + for (i = 0; i < words->len; ++i) + tmp[i] = words->buf[i]; + + mergesort(words->buf, tmp, words->len); +} + +static void uniq(struct strbuf *words) +{ + int i, j; + struct augstr *array = words->buf; + for (i = 0, j = 0; i < words->len; ++i) { + if (j > 0 && streq(&array[i].str, &array[j-1].str)) + continue; + array[j].str = array[i].str; + ++j; + } + words->len = j; +} + +static void expand_sort(struct eval_aux *aux, struct eprofile *prof) +{ + struct strbuf words; + struct str arg; + int i; + + heap_offset_save(); + extract_args(&arg, prof->body, 1, "sort", aux->linenum); + collect_words(&words, &arg); + + sort(&words); + uniq(&words); + + for (i = 0; i < words.len; ++i) { + if (i > 0) + buf_putchar(aux->mainbuf, ' '); + buf_puts(aux->mainbuf, &words.buf[i].str); + } + + heap_offset_restore(); +} + +static void save_shell_stdout(int fd, struct buf *buf) +{ + char tmpbuf[buf_static_size]; + struct str tmp = { buf->cur }; + int skip_spaces = 1; + for (;;) { + int i; + int ret = read(fd, tmpbuf, buf_static_size); + if (ret == -1) + syscall_error("shell output read"); + if (ret == 0) + break; + for (i = 0; i < ret; ++i) { + char c = tmpbuf[i]; + if (tmpbuf[i] == '\n') + c = ' '; + + if (is_space(c)) { + if (skip_spaces) + continue; + } else + skip_spaces = 1; + buf_putchar(buf, c); + } + } + tmp.end = buf->cur; + trim_back(&tmp); + buf->cur = tmp.end; + close(fd); +} +static void check_status(int status, char *prx); + +static void expand_shell(struct eval_aux *aux, struct eprofile *prof) +{ + struct str arg; + extract_args(&arg, prof->body, 1, "shell", aux->linenum); + int pipefds[2]; + int status, pid; + + trim_front(&arg); + if (pipe(pipefds) == -1) + syscall_error("pipe"); + pid = fork(); + if (pid == -1) + syscall_error("fork"); + if (pid == 0) { + char *argv[] = { "/bin/dash", "-c", arg.start, NULL }; + if (dup2(pipefds[1], 1) == -1) + syscall_error("dup2"); + close(pipefds[1]); + close(pipefds[0]); + execve(argv[0], argv, aux->world->envp); + exit(255); + } + close(pipefds[1]); + save_shell_stdout(pipefds[0], aux->mainbuf); + wait4(pid, &status, 0, NULL); + check_status(status, "shell: "); +} + +static void subst(struct buf *buf, struct str *from, struct str *to, + struct str *word) +{ + int i = 0; + int fromlen = strlen(from); + int n = strlen(word); + while (i <= n - fromlen) { + if (memeq(word->start + i, from->start, fromlen)) { + buf_puts(buf, to); + i += fromlen; + } else { + buf_putchar(buf, word->start[i]); + ++i; + } + } + for (; i < n; ++i) + buf_putchar(buf, word->start[i]); +} + +static void expand_subst(struct eval_aux *aux, struct eprofile *prof) +{ + struct str args[3]; + int first = 1; + + extract_args(args, prof->body, 3, "subst", aux->linenum); + + if (!arg_is_single_word(args)) + goto single_word_err; + if (!arg_is_single_word(args + 1)) + goto single_word_err; + + for (;; first = 0) { + struct str word; + int ret = extract_word(args + 2, &word); + if (ret == 0) + break; + if (!first) + buf_putchar(aux->mainbuf, ' '); + subst(aux->mainbuf, args, args + 1, &word); + } + return; +single_word_err: + lerror(aux->linenum, "subst: from/to must to single words"); +} + +static int matches(struct str *pat, struct str *str, + struct str *wildcard); + +static void patsubst(struct buf *buf, struct str *from, + struct str *to, struct str *words) +{ + int first = 1; + for (;; first = 0) { + struct str word, wildcard; + + if (extract_word(words, &word) == 0) + break; + if (!first) + buf_putchar(buf, ' '); + if (matches(from, &word, &wildcard)) { + char *tmp; + for (tmp = to->start; tmp != to->end; ++tmp) { + if (*tmp == '%') + buf_puts(buf, &wildcard); + else + buf_putchar(buf, *tmp); + } + } else { + buf_puts(buf, &word); + } + } +} + +static void expand_patsubst(struct eval_aux *aux, struct eprofile *prof) +{ + struct str args[3]; + extract_args(args, prof->body, 3, "patsubst", aux->linenum); + + if (!arg_is_single_word(args)) + goto single_word_err; + if (!arg_is_single_word(args + 1)) + goto single_word_err; + if (!contains_char(args, '%')) + goto no_wildcard_err; + if (!contains_char(args + 1, '%')) + goto no_wildcard_err; + + patsubst(aux->mainbuf, args, args + 1, args + 2); + return; + +single_word_err: + lerror(aux->linenum, "patsubst: from/to must be single words"); +no_wildcard_err: + lerror(aux->linenum, "patsubst: from/to must contain '%'"); +} + +enum { + max_limbs = 64 +}; + +struct pathbuf { + struct str limbuf[max_limbs]; + int limbs; + + char buf[path_max_len]; + char *cur; + + int add_space; + int linenum; + + struct buf *dest; +}; + +static void __pbuf_putchar(struct pathbuf *pbuf, char c, int inc) +{ + if (pbuf->cur == pbuf->buf + path_max_len) + error("pathbuf: no more space"); + *pbuf->cur = c; + if (inc) + ++pbuf->cur; +} + +static void pbuf_putchar(struct pathbuf *pbuf, char c) +{ + __pbuf_putchar(pbuf, c, 1); +} + +static void pbuf_put_silent_zero(struct pathbuf *pbuf) +{ + __pbuf_putchar(pbuf, 0, 0); +} + +static void add_slash(struct pathbuf *pbuf) +{ + if (pbuf->cur - pbuf->buf > 1 || pbuf->cur[-1] == '.') + pbuf_putchar(pbuf, '/'); +} + +static void pbuf_putlimb(struct pathbuf *pbuf, struct str *limb) +{ + char *tmp; + add_slash(pbuf); + for (tmp = limb->start; tmp != limb->end; ++tmp) + pbuf_putchar(pbuf, *tmp); +} + +static void pbuf_putlimbz(struct pathbuf *pbuf, char *limb) +{ + add_slash(pbuf); + for (; *limb; ++limb) + pbuf_putchar(pbuf, *limb); +} + +static void skip_slashes(struct str *path) +{ + char *tmp = path->start; + for (; tmp != path->end && *tmp == '/'; ++tmp) + ; + path->start = tmp; +} + +static void init_beginning(struct pathbuf *pbuf, struct str *path) +{ + if (path->start[0] == '/') { + skip_slashes(path); + pbuf_putchar(pbuf, '/'); + } else { + pbuf_putchar(pbuf, '.'); + } +} + +static struct str *get_word(struct pathbuf *pbuf) +{ + if (pbuf->limbs == max_limbs) + error("pathbuf: too deep nesting"); + return pbuf->limbuf + pbuf->limbs; +} + +static int eat_limb(struct pathbuf *pbuf, struct str *path) +{ + struct str *dest; + char *tmp; + if (strempty(path)) + return 0; + dest = get_word(pbuf); + dest->start = path->start; + for (tmp = path->start; tmp != path->end && *tmp != '/'; ++tmp) + ; + dest->end = tmp; + path->start = tmp; + skip_slashes(path); + ++pbuf->limbs; + return 1; +} + +static void parse_path(struct pathbuf *pbuf, struct str *path) +{ + init_beginning(pbuf, path); + while (eat_limb(pbuf, path)) + ; +} + +static int is_pattern(struct str *str) +{ + char *tmp = str->start; + for (; tmp != str->end; ++tmp) { + if (*tmp == '*' || *tmp == '?') + return 1; + } + return 0; +} + +static int is_special(char *dent) +{ + return dent[0] == '.' && + (dent[1] == 0 || (dent[1] == '.' && dent[2] == 0)); +} + +static int __match(char *pat, char *str, int line) +{ + for (; *pat; ++pat) { + if (*pat == '*') { + char *sc; + for (sc = str; ; ++sc) { + if (__match(pat + 1, sc, line)) + return 1; + if (*sc == 0) + return 0; + } + } else if (*pat == '?') { + if (*str) + ++str; + else + return 0; + } else if (*pat == '[') { + int ret = 0; + for (++pat; *pat && *pat != ']'; ++pat) { + if (*pat == *str) + ret = 1; + } + if (*pat == 0) + lerror(line, "wildcard missing ']'"); + if (ret == 0) + return 0; + ++str; + } else { + if (*pat != *str) + return 0; + ++str; + } + } + return *str == 0; +} + +static int match_pattern(struct str *pat, char *s, int line) +{ + char c = *pat->end; + int ret; + + *pat->end = 0; + + ret = __match(pat->start, s, line); + + *pat->end = c; + return ret; +} + +static void follow_path(struct pathbuf *pbuf, int start); + +static void iterate(struct pathbuf *pbuf, int idx) +{ + struct dir dir; + + heap_offset_save(); + + pbuf_put_silent_zero(pbuf); + if (opendir(&dir, pbuf->buf) == 0) + goto end; + + char *cur = pbuf->cur; + for (;;) { + struct dirent *dent = readdir(&dir); + pbuf->cur = cur; + + if (dent == NULL) + break; + if (is_special(dent->name)) + continue; + + int res = match_pattern(pbuf->limbuf + idx, dent->name, + pbuf->linenum); + if (res) { + pbuf_putlimbz(pbuf, dent->name); + follow_path(pbuf, idx + 1); + } + } + closedir(&dir); +end: + heap_offset_restore(); +} + +static void commit(struct buf *dest, char *path) +{ + char *tmp = path[0] == '/' ? path : path + 2; + buf_putsz(dest, tmp); +} + +static int exists(char *path); + +static void follow_path(struct pathbuf *pbuf, int start) +{ + int i; + for (i = start; i < pbuf->limbs; ++i) { + if (is_pattern(pbuf->limbuf + i)) { + iterate(pbuf, i); + return; + } else { + pbuf_putlimb(pbuf, pbuf->limbuf + i); + } + } + pbuf_put_silent_zero(pbuf); + if (exists(pbuf->buf)) { + if (pbuf->add_space) + buf_putchar(pbuf->dest, ' '); + commit(pbuf->dest, pbuf->buf); + pbuf->add_space = 1; + } +} + +static int wexpand(struct buf *buf, struct str *path, int add_space, + int linenum) +{ + static struct pathbuf pbuf; + pbuf.limbs = 0; + pbuf.cur = pbuf.buf; + pbuf.add_space = add_space; + pbuf.dest = buf; + pbuf.linenum = linenum; + + parse_path(&pbuf, path); + follow_path(&pbuf, 0); + return pbuf.add_space; +} + +static void expand_wildcard(struct eval_aux *aux, struct eprofile *prof) +{ + struct str arg; + int first = 1; + + extract_args(&arg, prof->body, 1, "wildcard", aux->linenum); + for (;;) { + struct str word; + if (extract_word(&arg, &word) == 0) + break; + int ret = wexpand(aux->mainbuf, &word, !first, + aux->linenum); + if (ret && first) + first = 0; + } +} + +static void expand_func(struct eval_aux *aux, struct eprofile *prof) +{ + char *savedpos = aux->auxbuf->cur; + eval_wrapper(aux, prof->body, 1, 1); + prof->body = &(struct str) { savedpos, aux->auxbuf->cur }; + + switch(prof->type) { + case id_func_basename: + expand_basename_or_dir(aux, mode_basename, prof); + break; + case id_func_dir: + expand_basename_or_dir(aux, mode_dir, prof); + break; + case id_func_addprefix: + expand_add(aux, mode_prefix, prof); + break; + case id_func_addsuffix: + expand_add(aux, mode_suffix, prof); + break; + case id_func_filter: + expand_filter(aux, mode_filter, prof); + break; + case id_func_filter_out: + expand_filter(aux, mode_filter_out, prof); + break; + case id_func_sort: + expand_sort(aux, prof); + break; + case id_func_shell: + expand_shell(aux, prof); + break; + case id_func_subst: + expand_subst(aux, prof); + break; + case id_func_patsubst: + expand_patsubst(aux, prof); + break; + case id_func_wildcard: + expand_wildcard(aux, prof); + break; + default: + error("func id=%d not implemented yet", prof->type); + } + aux->auxbuf->cur = savedpos; +} + +static void __expand(struct eval_aux *aux, struct eprofile *prof) +{ + switch(prof->type) { + case id_variable: + expand_variable(aux, prof); + break; + case id_subref: + expand_subref(aux, prof); + break; + case id_target: + expand_target(aux); + break; + case id_first_prereq: + expand_first_prereq(aux, prof); + break; + case id_prereqs: + expand_prereqs(aux, prof); + break; + default: + expand_func(aux, prof); + break; + } +} + +static void expand(struct eval_aux *aux, struct str *str) +{ + struct eprofile profile; + struct str estr; + + if (strempty(str)) + lerror(aux->linenum, "stray '$'"); + + if (*str->start == '(') { + char *end = find_cparent(aux->linenum, str); + + ++str->start; + estr = (struct str) { str->start, end }; + str->start = end + 1; + } else { + estr = (struct str) { str->start, str->start + 1 }; + ++str->start; + } + parse_profile(&profile, &estr, aux->linenum, aux->rule); + __expand(aux, &profile); +} + +static int stop_sym(char c) +{ + return c == '=' || c == '+' || c == ':'; +} + +static void __eval(struct eval_aux *aux, struct str *str) +{ + while (str->start != str->end) { + if (stop_sym(*str->start) && aux->level == 0 && + aux->rule == NULL) + { + return; + } else if (*str->start == '$') { + ++str->start; + expand(aux, str); + } else { + buf_putchar(aux->mainbuf, *str->start); + ++str->start; + } + } +} + +static struct buf *evict_buf(struct buf *buf) +{ + static struct buf tmp; + tmp = *buf; + tmp.end = tmp.cur; + buf->start = buf->cur; + return &tmp; +} + +static struct buf *eval(struct world *world, struct line *line, + struct full_rule *rule) +{ + static char localbuf2[buf_size]; + static struct buf buffers[2] = { + { NULL, NULL, NULL }, + { NULL, NULL, NULL } + }; + struct eval_aux aux; + + if (buffers[0].start == NULL) + buf_init(buffers); + else + buffers[0].cur = buffers[0].start; + buf_static_init(buffers[1], localbuf2, buf_size); + + aux = (struct eval_aux) { + 0, + line->linenum, + rule, + buffers, buffers + 1, + world + }; + __eval(&aux, &line->str); + buf_putchar(buffers, ' '); + + return rule == NULL ? evict_buf(buffers) : buffers; +} + +static void trim_front(struct str *str) +{ + for (; str->start != str->end; ++str->start) { + if (!is_space(*str->start)) + break; + } +} + +static void trim_back(struct str *str) +{ + for (; str->start != str->end; --str->end) { + if (!is_space(str->end[-1])) + break; + } +} + +static void trim(struct str *str) +{ + trim_front(str); + trim_back(str); +} + +static int valid_var_name(struct str *vname) +{ + char *tmp; + for (tmp = vname->start; tmp != vname->end; ++tmp) { + if (!valid_var_char(*tmp)) + return 0; + } + return 1; +} + +static void str_combine(struct str *res, struct str *s1, struct str *s2) +{ + int len1, len2, nlen; + + if (s1 == NULL) { + *res = *s2; + return; + } + len1 = strlen(s1); + len2 = strlen(s2); + nlen = len1 + len2 + 1; + + res->start = malloc(nlen); + res->end = res->start + nlen; + + memcpy(res->start, s1->start, len1); + res->start[len1] = ' '; + memcpy(res->start + len1 + 1, s2->start, len2); +} + +enum { + svmode_set, + svmode_append +}; + +static void make_full_vname(struct buf *dest, struct str *prx, + struct str *vname) +{ + dest->cur = dest->start; + buf_puts(dest, prx); + buf_putchar(dest, '#'); + buf_puts(dest, vname); +} + +static void set_single_var(struct vtable *vt, int mode, struct str *prefix, + struct str *vname,struct str *value, int alloc_name) +{ + static char localbuf[buf_static_size]; + struct buf buf; + struct str tmp; + + buf_static_init(buf, localbuf, buf_static_size); + + if (prefix != NULL) { + make_full_vname(&buf, prefix, vname); + tmp = (struct str) { buf.start, buf.cur }; + } else { + tmp = *vname; + } + if (mode == svmode_set) { + vtable_update(vt, &tmp, alloc_name, value); + } else { + struct str newval; + struct str *oldval = vtable_find(vt, &tmp); + if (oldval == NULL) + oldval = vtable_find(vt, vname); + str_combine(&newval, oldval, value); + vtable_update(vt, &tmp, alloc_name, &newval); + } +} + +static void setvar(struct vtable *vt, int mode, struct strbuf *targets, + struct str *vname, struct str *value) +{ + trim(value); + + if (targets != NULL) { + int i; + for (i = 0; i < targets->len; ++i) { + set_single_var(vt, mode, &targets->buf[i].str, vname, value, 1); + } + } else { + set_single_var(vt, mode, NULL, vname, value, 0); + } +} + +static int is_followed(struct line *line, char c) +{ + return strlen(&line->str) >= 2 && line->str.start[1] == c; +} + +static void follows(struct line *line, char c) +{ + if (is_followed(line, c)) { + line->str.start += 2; + return; + } + lerror(line->linenum, "expected %c after %c", c, + line->str.start[0]); +} + +static void parse_assignment(struct world *world, struct strbuf *targets, + struct buf *buf, struct line *line) +{ + struct str vname = { buf->start, buf->cur }; + + trim(&vname); + if (!valid_var_name(&vname)) { + lerror(line->linenum, "[%S]: invalid variable name", + &vname); + } + if (line->str.start[0] == '+') { + follows(line, '='); + setvar(&world->vtable, svmode_append, targets, &vname, + &line->str); + } else { + ++line->str.start; + setvar(&world->vtable, svmode_set, targets, &vname, + &line->str); + } +} + +static void sbuf_init(struct strbuf *buf) +{ + buf->size = 1; + buf->len = 0; + buf->buf = malloc(sizeof(*buf->buf) * buf->size); +} + +static void sbuf_save(struct strbuf *buf, struct str *str, int aux) +{ + if (buf->len == buf->size) { + buf->size *= 2; + realloc(buf->buf, sizeof(*buf->buf) * buf->size); + } + buf->buf[buf->len].str = *str; + buf->buf[buf->len].auxval = aux; + ++buf->len; +} + +static int valid_target_char(char c) +{ + return valid_var_char(c) || c == '%'; +} + +static void save_word(struct strbuf *buf, struct str *str, int line) +{ + struct str word; + word.start = str->start; + + /* No check for empty string here, because str is part of eval + * result. It won't be overwritten in this case. And at the end + * of eval we add ' ' for this function. + */ + for (;; ++str->start) { + if (is_space(*str->start) || *str->start == ':') + break; + if (!valid_target_char(*str->start)) + goto err; + } + word.end = str->start; + *str->start = 0; + ++str->start; + + sbuf_save(buf, &word, line); + return; +err: + lerror(line, "%c: invalid target/prereq name symbol", *str->start); +} + +static void parse_targets(struct strbuf *targets, struct buf *buf, int line) +{ + struct str str = { buf->start, buf->cur }; + sbuf_init(targets); + + for (;;) { + trim_front(&str); + if (strempty(&str)) + break; + save_word(targets, &str, -1); + } + if (targets->len == 0) + lerror(line, "zero targets before ':'"); +} + +static void parse_prereqs(struct rule_body *rule, struct buf *buf) +{ + struct str str = { buf->start, buf->cur }; + sbuf_init(&rule->prereqs); + int i = 0; + int normal = -1; + + for (;;) { + trim_front(&str); + if (strempty(&str)) + break; + if (*str.start == '|' && normal == -1) { + normal = i; + ++str.start; + trim_front(&str); + } + save_word(&rule->prereqs, &str, -1); + ++i; + } + rule->normal_prereq_count = normal == -1 ? i : normal; +} + +static struct strbuf *parse_recipes(char **file) +{ + struct strbuf *tmp = malloc(sizeof(*tmp)); + sbuf_init(tmp); + + while (nextline_starts_with_tab(*file)) { + struct line line; + readline(file, &line); + + trim(&line.str); + sbuf_save(tmp, &line.str, line.linenum); + } + return tmp; +} + +static void save_first_target(struct world *world, + struct rule_details *rule) +{ + if (!strempty(&world->target) || rule->patrule || + streq(&rule->targets.buf->str, &CSTR(".PRECIOUS"))) + { + return; + } + world->target = rule->targets.buf->str; +} + +static char *contains_char(struct str *str, char c) +{ + char *tmp; + for (tmp = str->start; tmp != str->end; ++tmp) { + if (*tmp == c) + return tmp; + } + return NULL; +} + +static int is_patrule(struct strbuf *targets, int line) +{ + if (contains_char(&targets->buf[0].str, '%') == NULL) + return 0; + if (targets->len == 1) + return 1; + lerror(line, "multiple targets not supported in patturn rules"); + return -1; /* unreachable */ +} + +static void add_patrule(struct world *world, struct rule_details *rule) +{ + if (world->pat_rule_num == pat_rules_limit) + lerror(rule->lineno, "pattern rules limit reached"); + world->pat_rules[world->pat_rule_num] = (struct full_rule) { + &rule->targets.buf[0].str, + rule->body + }; + ++world->pat_rule_num; +} + +static void handle_normal(struct world *world, struct rule_details *rule, + struct buf *buf) +{ + int i; + parse_prereqs(rule->body, buf); + rule->body->recipes = parse_recipes(&world->makefile); + + if (rule->patrule) { + add_patrule(world, rule); + return; + } + + for (i = 0; i < rule->targets.len; ++i) { + if (streq(&rule->targets.buf[i].str, &CSTR(".PRECIOUS"))) + continue; + rtable_add(&world->rtable, &rule->targets.buf[i].str, 0, + rule->body); + } +} + +char *target_spec_in_patrules = + "target-specific var in pattern rules are not supported"; + +static void handle_target_spec(struct world *world, struct buf *buf, + struct line *line, struct rule_details *rule) +{ + if (rule->patrule) + lerror(line->linenum, target_spec_in_patrules); + if (*line->str.start == ':') + lerror(line->linenum, "expected target-specific var"); + + parse_assignment(world, &rule->targets, buf, line); +} + +static void parse_rule(struct world *world, struct buf *buf, + struct line *line) +{ + struct rule_details rule; + + ++line->str.start; + rule.body = malloc(sizeof(*rule.body)); + rule.lineno = line->linenum; + + parse_targets(&rule.targets, buf, line->linenum); + rule.patrule = is_patrule(&rule.targets, line->linenum); + + buf = eval(world, line, NULL); + if (strempty(&line->str)) + handle_normal(world, &rule, buf); + else + handle_target_spec(world, buf, line, &rule); + + save_first_target(world, &rule); +} + +static int parse_toplev(struct world *world) +{ + struct line line; + struct buf *buf; + + if (!readline(&world->makefile, &line)) + return 0; + buf = eval(world, &line, NULL); + + if (strempty(&line.str)) { + struct str tmp = { buf->start, buf->cur }; + trim(&tmp); + if (!strempty(&tmp)) + lerror(line.linenum, "missing separator"); + } else if (*line.str.start == ':') { + parse_rule(world, buf, &line); + } else { + parse_assignment(world, NULL, buf, &line); + } + return 1; +} + +static void parse(struct world *world) +{ + while (parse_toplev(world)) + ; +} + +static void world_init(struct world *world, struct conf *conf, char **envp) +{ + world->makefile = mmap_makefile(conf->makefile); + world->target = conf->target ? STR(conf->target) : CSTR(""); + world->envp = envp; + + world->pat_rule_num = 0; + world->debug_mode = conf->debug_mode; + + vtable_init(&world->vtable); + rtable_init(&world->rtable); +} + +static int timegt(struct time *t1, struct time *t2) +{ + if (t1->sec > t2->sec) + return 1; + if (t1->sec == t2->sec) + return t1->usec > t2->usec; + return 0; +} + +static void check_status(int status, char *prx) +{ + if (WIFEXITED(status)) { + int ret = WEXITSTATUS(status); + if (ret != 0) { + error("%s: program exited with %d status", + prx, ret); + } + } else if(WIFSIGNALED(status)) { + error("%s: program received a signal %d", + WTERMSIG(status), prx); + } else { + error("%s: unknown program status", prx); + } +} + +static void exec_recipe(struct world *world, struct full_rule *rule, + int i) +{ + char *argv[] = { "/bin/sh", "-c", NULL, NULL }; + int ret; + int status; + struct augstr *astr = rule->rest->recipes->buf + i; + struct line line = { astr->auxval, astr->str }; + + struct buf *buf = eval(world, &line, rule); + buf_put_silent_zero(buf); + + argv[2] = buf->start; + flush(&stderr); + ret = fork(); + + if (ret == -1) + syscall_error("fork"); + if (ret == 0) { + struct str cmd = { buf->start, buf->cur }; + trim(&cmd); + printf("%S\n", &cmd); + execve("/bin/sh", argv, world->envp); + + perror("execve"); + flush(&stderr); + exit(1); + } + wait4(ret, &status, 0, 0); + check_status(status, ""); +} + +static void exec_recipes(struct world *world, struct full_rule *rule) +{ + int i; + for (i = 0; i < rule->rest->recipes->len; ++i) + exec_recipe(world, rule, i); +} + +static int exists(char *path) +{ + return access(path, F_OK) == 0; +} + +static int matches(struct str *pat, struct str *str, + struct str *wildcard) +{ + char *tmp = pat->start; + char *stmp = str->start; + int rem; + for (;*tmp != '%'; ++tmp, ++stmp) { + if (*tmp != *stmp) + return 0; + } + ++tmp; + + rem = pat->end - tmp; + if (rem > str->end - stmp) + return 0; + wildcard->start = stmp; + wildcard->end = str->end - rem; + return memeq(tmp, wildcard->end, rem); +} + +static void heapbuf_init(struct buf *buf) +{ + buf->start = malloc(1); + buf->cur = buf->start; + buf->end = buf->start + 1; +} + +static void heapbuf_putchar(struct buf *buf, char c) +{ + if (buf->cur == buf->end) { + ++buf->end; + realloc(buf->start, buf->end - buf->start); + } + *buf->cur = c; + ++buf->cur; +} + +static void heapbuf_puts(struct buf *buf, struct str *str) +{ + char *tmp; + for (tmp = str->start; tmp != str->end; ++tmp) + heapbuf_putchar(buf, *tmp); +} + +static void subst_pat(struct str *dest, struct str *pat, + struct str *wildcard) +{ + struct buf buf; + char *tmp; + heapbuf_init(&buf); + for (tmp = pat->start; tmp != pat->end; ++tmp) { + if (*tmp != '%') + heapbuf_putchar(&buf, *tmp); + else + heapbuf_puts(&buf, wildcard); + } + dest->start = buf.start; + dest->end = buf.cur; + heapbuf_putchar(&buf, 0); +} + +static struct rule_body *construct_prereqs(struct full_rule *patrule, + struct str *wildcard) +{ + struct rule_body *newbody = malloc(sizeof(*newbody)); + struct strbuf *prereqs = &newbody->prereqs; + int i; + + newbody->normal_prereq_count = patrule->rest->normal_prereq_count; + newbody->recipes = patrule->rest->recipes; + + prereqs->len = patrule->rest->prereqs.len; + prereqs->buf = malloc(sizeof(*prereqs->buf) * prereqs->len); + for (i = 0; i < prereqs->len; ++i) { + subst_pat(&prereqs->buf[i].str, + &patrule->rest->prereqs.buf[i].str, wildcard); + } + return newbody; +} + +static void ccarena_grow(struct arena *a, int size) +{ + error("ccarena: no more space"); +} + +void prereqdup(struct arena *a, struct str *dest, struct str *src) +{ + int len = strlen(src); + dest->start = amalloc(a, len + 1); + dest->end = dest->start + len; + memcpy(dest->start, src->start, len); + *dest->end = 0; +} + +static struct rule_body *create_copy(struct rule_body *rule_body) +{ + static struct arena ccarena; + struct rule_body *tmp; + struct strbuf *prereqs; + int i; + if (ccarena.start == NULL) { + ccarena.start = ccarena.cur = mem_alloc(buf_size); + ccarena.end = ccarena.start + buf_size; + ccarena.grow = ccarena_grow; + } + tmp = amalloc(&ccarena, sizeof(*tmp)); + tmp->normal_prereq_count = rule_body->normal_prereq_count; + tmp->recipes = rule_body->recipes; + prereqs = &tmp->prereqs; + prereqs->len = rule_body->prereqs.len; + prereqs->buf = + amalloc(&ccarena, sizeof(*prereqs->buf) * prereqs->len); + + for (i = 0; i < prereqs->len; ++i) { + prereqdup(&ccarena, &prereqs->buf[i].str, + &rule_body->prereqs.buf[i].str); + } + /*printf(">>> Cur: %d\n", ccarena.cur - ccarena.start);*/ + return tmp; +} + +static struct rule_body *find_body(struct world *world, struct str *target, + int depth); + +static struct rule_body *suitable_patrule(struct world *world, int idx, + struct str *target, int depth) +{ + struct rule_body *tmp = NULL; + struct rule_body *tmp_copy; + struct str wildcard; + int i; + + heap_offset_save(); + + if (!matches(world->pat_rules[idx].target, target, &wildcard)) + goto failure; + + tmp = construct_prereqs(world->pat_rules + idx, &wildcard); + + for (i = 0; i < tmp->prereqs.len; ++i) { + struct str *prereq = &tmp->prereqs.buf[i].str; + struct rule_body *pbody; + if (exists(prereq->start)) + continue; + pbody = find_body(world, prereq, depth); + if (pbody == NULL) + goto failure; + } + tmp_copy = create_copy(tmp); + /*printf(">>> Saving: %S\n", target);*/ + rtable_add(&world->rtable, target, 1, tmp_copy); + heap_offset_remove(); + return tmp_copy; +failure: + heap_offset_restore(); + return NULL; +} + +static struct rule_body *find_body(struct world *world, + struct str *target, int depth) +{ + int i; + struct rule_body *tmp; + /*printf("FB: %S\n", target);*/ + if (rtable_find(&world->rtable, target, &tmp) == 1) + return tmp; + + if (depth >= find_body_max_depth) + return NULL; + + for (i = 0; i < world->pat_rule_num; ++i) { + tmp = suitable_patrule(world, i, target, depth + 1); + if (tmp != NULL) + break; + } + if (tmp == NULL) + rtable_add(&world->rtable, target, 1, NULL); + return tmp; +} + +struct target_info { + struct time mtime; + int exists; +}; + +static int get_mtime(struct str *str, struct time *time) +{ + int ret; + struct stat st; + + ret = stat(str->start, &st); + /*printf("Time: %s %d\n", str->start, ret);*/ + if (ret == -1) { + if (errno == ENOENT) { + return 0; + } + syscall_error(str->start); + } + *time = st.mtime; + return 1; +} + +static void tinfo_get(struct target_info *info, struct str *str) +{ + info->exists = get_mtime(str, &info->mtime); +} + +static int exec_target(struct world *world, struct str *target, + struct target_info *info); + +struct pstatus { + struct time time; + struct str *prereq; +}; + +static int exec_prereqs(struct world *world, struct full_rule *rule, + struct pstatus *pstatus, struct target_info *info) +{ + int i; + int flag = 0; + struct target_info pinfo; + + *pstatus = (struct pstatus) { { 0, 0 }, NULL }; + + for (i = 0; i < rule->rest->prereqs.len; ++i) { + int ret, is_oo; + struct str *prereq = &rule->rest->prereqs.buf[i].str; + tinfo_get(&pinfo, prereq); + ret = exec_target(world, prereq, &pinfo); + is_oo = i >= rule->rest->normal_prereq_count; + if (ret) { + if (flag == 0) + pstatus->prereq = prereq; + flag = 1; + } + if (flag == 0 && !is_oo && timegt(&pinfo.mtime, &pstatus->time)) { + pstatus->time = pinfo.mtime; + pstatus->prereq = prereq; + } + } + return flag; +} + +static void print_reason(int debug, struct str *target, int flag, + struct target_info *info, struct pstatus *ptime) +{ + + if (debug == 0) + return; + if (!info->exists) { + printf("%s%S did not exist\n", debug_prx, target); + } else if (flag) { + printf("%s%S(%U.%U) by %S\n", debug_prx, target, + info->mtime.sec, info->mtime.usec, + ptime->prereq); + } else { + printf("%s%S(%U.%U) <- %S(%U.%U)\n", debug_prx, target, + info->mtime.sec, info->mtime.usec, + ptime->prereq, ptime->time.sec, + ptime->time.usec); + } +} + +static void print_new_time(int debug, struct str *target) +{ + struct time time; + if (debug == 0) + return; + if (get_mtime(target, &time)) { + printf("%sUpdated time %S: %U.%U\n", debug_prx, target, + time.sec, time.usec); + } else { + printf("%s%S was not created\n", debug_prx, target); + } +} + +static int exec_target(struct world *world, struct str *target, + struct target_info *info) +{ + struct rule_body *prule = find_body(world, target, 0); + struct full_rule rule = { target, prule }; + int flag = 0; + struct pstatus pstatus; + int prereq_later; + + /*printf("EXEC: %S\n", target);*/ + if (prule == NULL) { + if (info->exists) { + return 0; + } else { + error("%S: don't know what to do with the target", + target); + } + } + flag = exec_prereqs(world, &rule, &pstatus, info); + + prereq_later = timegt(&pstatus.time, &info->mtime); + if (flag || !info->exists || prereq_later) { + print_reason(world->debug_mode, target, flag, info, + &pstatus); + exec_recipes(world, &rule); + print_new_time(world->debug_mode, target); + return 1; + } + return 0; +} + +static void exec(struct world *world) +{ + struct target_info info; + if (strempty(&world->target)) { + printf("nothing to be done"); + hexit(0); + } + tinfo_get(&info, &world->target); + exec_target(world, &world->target, &info); +} + +static void print_program_time(int debug) +{ + struct rusage rusage; + if (!debug) + return; + if (getrusage(RUSAGE_SELF, &rusage) == -1) + syscall_error("rusage"); + printf("%s: user time: %U.%3U, sys time: %U.%3U\n", progname, + rusage.utime.sec, rusage.utime.msec / 1000, + rusage.stime.sec, rusage.stime.msec / 1000); +} + +int main(int argc, char **argv, char **envp) +{ + struct world world; + struct conf conf = { NULL, NULL }; + + parse_cmdargs(&conf, argv); + + world_init(&world, &conf, envp); + parse(&world); + + exec(&world); + print_program_time(world.debug_mode); + return 0; +} diff --git a/tools/sed.c b/tools/sed.c index fd60a07..c427acd 100644 --- a/tools/sed.c +++ b/tools/sed.c @@ -96,7 +96,6 @@ struct threadlist { struct ins *ref; struct node **presence_table; - }; struct input_buf { @@ -117,16 +116,6 @@ struct compiled_command { static struct compiled_command command_buf[max_commands]; static int command_num = 0; -static void error(char *fmt, ...) -{ - va_list vl; - va_start(vl, fmt); - evprintf(fmt, vl); - va_end(vl); - print_strz(&stderr, "\n"); - hexit(1); -} - static void inbuf_init() { input_buf.start = input_buf.end = input_buf.buf; @@ -818,6 +807,7 @@ static char **exec_threadlist(struct threadlist *clist, struct threadlist *nlist, char c, char *offset) { struct thread tmp; + struct thread tmp2; while (threadlist_getfront(clist, &tmp)) { start: switch(tmp.pc->opcode) { @@ -847,7 +837,7 @@ start: tmp.pc = tmp.pc->arg_labels[0]; goto start; case op_split: - struct thread tmp2 = thread_copy(&tmp); + tmp2 = thread_copy(&tmp); tmp2.pc = tmp.pc->arg_labels[1]; tmp.pc = tmp.pc->arg_labels[0]; threadlist_addfront(clist, tmp2); @@ -888,9 +878,9 @@ static char **__regex_exec(struct regex_prog *prog, struct str *str) static char **regex_exec(struct regex_prog *prog, struct str *str) { char **ret; - save_heap_offset(); + heap_offset_save(); ret = __regex_exec(prog, str); - restore_heap_offset(); + heap_offset_restore(); return ret; } |