From 846a95f687048775d8572b62ffc839d0fcc009f4 Mon Sep 17 00:00:00 2001 From: Stephane Desneux Date: Mon, 9 Dec 2013 10:55:25 +0100 Subject: [PATCH] version 1.0 Change-Id: Ie8f0c6fd9c81c0f4b5fc0f04a6c8276954f45cbd Signed-off-by: Stephane Desneux --- .gitignore | 1 + LGPL_2.0 | 481 +++++++++++++++++++ Makefile.am | 3 + README | 11 + autogen.sh | 7 + configure.ac | 35 ++ packaging/tizen-platform-wrapper.manifest | 5 + packaging/tizen-platform-wrapper.spec | 31 ++ src/Makefile.am | 6 + src/buffer.c | 119 +++++ src/buffer.h | 47 ++ src/foreign.c | 90 ++++ src/foreign.h | 70 +++ src/heap.c | 140 ++++++ src/heap.h | 99 ++++ src/parser.c | 319 +++++++++++++ src/parser.h | 112 +++++ src/passwd.c | 280 +++++++++++ src/passwd.h | 39 ++ src/scratch.c | 157 +++++++ src/scratch.h | 50 ++ src/toolbox.c | 746 ++++++++++++++++++++++++++++++ src/tzplatform_config.c | 599 ++++++++++++++++++++++++ src/tzplatform_config.h | 173 +++++++ src/tzplatform_get.c | 188 ++++++++ 25 files changed, 3808 insertions(+) create mode 100644 LGPL_2.0 create mode 100644 Makefile.am create mode 100644 README create mode 100755 autogen.sh create mode 100644 configure.ac create mode 100644 packaging/tizen-platform-wrapper.manifest create mode 100644 packaging/tizen-platform-wrapper.spec create mode 100644 src/Makefile.am create mode 100644 src/buffer.c create mode 100644 src/buffer.h create mode 100644 src/foreign.c create mode 100644 src/foreign.h create mode 100644 src/heap.c create mode 100644 src/heap.h create mode 100644 src/parser.c create mode 100644 src/parser.h create mode 100644 src/passwd.c create mode 100644 src/passwd.h create mode 100644 src/scratch.c create mode 100644 src/scratch.h create mode 100644 src/toolbox.c create mode 100644 src/tzplatform_config.c create mode 100644 src/tzplatform_config.h create mode 100644 src/tzplatform_get.c diff --git a/.gitignore b/.gitignore index 726c3ca..3a7ef20 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .*.sw* +*~ diff --git a/LGPL_2.0 b/LGPL_2.0 new file mode 100644 index 0000000..8ba60a1 --- /dev/null +++ b/LGPL_2.0 @@ -0,0 +1,481 @@ + GNU LIBRARY GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the library GPL. It is + numbered 2 because it goes with version 2 of the ordinary GPL.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Library General Public License, applies to some +specially designated Free Software Foundation software, and to any +other libraries whose authors decide to use it. You can use it for +your libraries, 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 +this service 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 make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if +you distribute copies of the library, or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link a program with the library, you must provide +complete object files to the recipients so that they can relink them +with the library, after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + Our method of protecting your rights has two steps: (1) copyright +the library, and (2) offer you this license which gives you legal +permission to copy, distribute and/or modify the library. + + Also, for each distributor's protection, we want to make certain +that everyone understands that there is no warranty for this free +library. If the library is modified by someone else and passed on, we +want its recipients to know that what they have is not the original +version, so that any problems introduced by others will not reflect on +the original authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that companies distributing free +software will individually obtain patent licenses, thus in effect +transforming the program into proprietary software. To prevent this, +we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + + Most GNU software, including some libraries, is covered by the ordinary +GNU General Public License, which was designed for utility programs. This +license, the GNU Library General Public License, applies to certain +designated libraries. This license is quite different from the ordinary +one; be sure to read it in full, and don't assume that anything in it is +the same as in the ordinary license. + + The reason we have a separate public license for some libraries is that +they blur the distinction we usually make between modifying or adding to a +program and simply using it. Linking a program with a library, without +changing the library, is in some sense simply using the library, and is +analogous to running a utility program or application program. However, in +a textual and legal sense, the linked executable is a combined work, a +derivative of the original library, and the ordinary General Public License +treats it as such. + + Because of this blurred distinction, using the ordinary General +Public License for libraries did not effectively promote software +sharing, because most developers did not use the libraries. We +concluded that weaker conditions might promote sharing better. + + However, unrestricted linking of non-free programs would deprive the +users of those programs of all benefit from the free status of the +libraries themselves. This Library General Public License is intended to +permit developers of non-free programs to use free libraries, while +preserving your freedom as a user of such programs to change the free +libraries that are incorporated in them. (We have not seen how to achieve +this as regards changes in header files, but we have achieved it as regards +changes in the actual functions of the Library.) The hope is that this +will lead to faster development of free libraries. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, while the latter only +works together with the library. + + Note that it is possible for a library to be covered by the ordinary +General Public License rather than by this special one. + + GNU LIBRARY GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library which +contains a notice placed by the copyright holder or other authorized +party saying it may be distributed under the terms of this Library +General Public License (also called "this License"). Each licensee is +addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also compile or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + c) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + d) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the source code distributed need not include anything that is normally +distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +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 +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Library 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 Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "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 +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY 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 +LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..6b75694 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,3 @@ + +SUBDIRS = src + diff --git a/README b/README new file mode 100644 index 0000000..0997e30 --- /dev/null +++ b/README @@ -0,0 +1,11 @@ +This is a tool for designing and using /etc/tizen-platform.conf file. + +It produces the program tzplatform-tool that allow users to check and +pretty format the /etc/tizen-platform.conf file. That tool is also used +to generate header and source file for processing /etc/tizen-platform.conf +file. + +It also exports files to handle /etc/tizen-platform.conf file. These are +used by some other projetcs as tize-platform-config that are the +implementation. + diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..9da86f7 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +test -n "$srcdir" || srcdir=`dirname "$0"` +test -n "$srcdir" || srcdir=. + +autoreconf --force --install --verbose "$srcdir" +test -n "$NOCONFIGURE" || "$srcdir/configure" "$@" diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..4acb89c --- /dev/null +++ b/configure.ac @@ -0,0 +1,35 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ([2.69]) +AC_INIT([tizen-platform-wrapper], [1.0]) +AM_INIT_AUTOMAKE([-Wall -Werror foreign]) +AC_CONFIG_SRCDIR([src/parser.h]) +AC_CONFIG_HEADERS([src/config.h]) + +# Checks for programs. +AC_PROG_CC +AC_PROG_INSTALL + +# Checks for libraries. + +# Checks for header files. +AC_FUNC_ALLOCA +AC_CHECK_HEADERS([fcntl.h stdint.h stdlib.h string.h unistd.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_INLINE +AC_TYPE_OFF_T +AC_TYPE_PID_T +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_ERROR_AT_LINE +AC_FUNC_FORK +AC_FUNC_MALLOC +AC_FUNC_MMAP +AC_CHECK_FUNCS([dup2 endpwent munmap strndup]) + +AC_CONFIG_FILES([Makefile + src/Makefile]) +AC_OUTPUT diff --git a/packaging/tizen-platform-wrapper.manifest b/packaging/tizen-platform-wrapper.manifest new file mode 100644 index 0000000..017d22d --- /dev/null +++ b/packaging/tizen-platform-wrapper.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/tizen-platform-wrapper.spec b/packaging/tizen-platform-wrapper.spec new file mode 100644 index 0000000..ae84d2c --- /dev/null +++ b/packaging/tizen-platform-wrapper.spec @@ -0,0 +1,31 @@ +Name: tizen-platform-wrapper +Version: 1.0 +Release: 0 +License: LGPL-2.0 +Summary: A toolkit to generate the libtizen-platform-config library +Url: http://www.tizen.org +Group: Development/Tools +Source: %{name}-%{version}.tar.bz2 +Source1001: %{name}.manifest +Requires: gperf + +%description +A toolkit to generate the libtizen-platform-config library in tizen-platform-config. + +%prep +%setup -q +cp %{SOURCE1001} . + +%build +%autogen +make %{?_smp_mflags} + +%install +%make_install + +%files +%manifest %{name}.manifest +%license LGPL_2.0 +%{_bindir}/* +%{_datadir}/%{name}/* + diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..f65b06d --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,6 @@ +bin_PROGRAMS = tzplatform-tool + +tzplatform_tool_SOURCES = buffer.c foreign.c heap.c parser.c toolbox.c + +dist_pkgdata_DATA = buffer.c buffer.h foreign.c foreign.h heap.c heap.h parser.c parser.h scratch.c scratch.h tzplatform_config.c tzplatform_config.h tzplatform_get.c passwd.h passwd.c + diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 0000000..1a72e76 --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ + +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include + +#include "buffer.h" + +static int buffread( struct buffer *buffer, int fd) +{ + const int size = 4096; + void *p; + char *memory = NULL; + size_t length = 0; + ssize_t status; + + /* do read */ + for(;;) { + p = realloc( memory, length + size); + if (p == NULL) { + free( memory); + return -1; + } + memory = p; + status = read( fd, memory+length, size); + if (status == 0) { + buffer->buffer = memory; + buffer->length = length; + buffer->mapped = 0; + return 0; + } + if (status > 0) { + length = length + (int)status; + } + else if (errno != EAGAIN && errno != EINTR) { + free( memory); + return -1; + } + } +} + +int buffer_create( struct buffer *buffer, const char *pathname) +{ + int fd, result; + struct stat bstat; + void *memory; + size_t length; + + result = open(pathname, O_RDONLY); + if (result >= 0) + { + fd = result; + result = fstat(fd, &bstat); + if (result == 0) + { + length = (size_t)bstat.st_size; + if (bstat.st_size != (off_t)length) + { + errno = EOVERFLOW; + result = -1; + } + else + { + memory = mmap(NULL,length,PROT_READ,MAP_PRIVATE,fd,0); + if (memory != MAP_FAILED) { + buffer->buffer = memory; + buffer->length = length; + buffer->mapped = 1; + } + else { + result = buffread( buffer, fd); + } + } + } + close(fd); + } + return result; +} + +int buffer_destroy( struct buffer *buffer) +{ + if (buffer->mapped) + return munmap(buffer->buffer, buffer->length); + free( buffer->buffer); + return 0; +} + + diff --git a/src/buffer.h b/src/buffer.h new file mode 100644 index 0000000..cd86040 --- /dev/null +++ b/src/buffer.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef BUFFER_H +#define BUFFER_H + +/* structure of the buffer */ +struct buffer { + char *buffer; /* start address */ + size_t length; /* length in byte */ + int mapped; /* is memory mapped */ +}; + +/* + Create the 'buffer' from reading content of the file of 'pathname'. + Returns 0 if success, -1 if error occured (see then errno) +*/ +int buffer_create( struct buffer *buffer, const char *pathname); + +/* + Destroy the 'buffer'. + Returns 0 if success, -1 if error occured (see then errno) +*/ +int buffer_destroy( struct buffer *buffer); + +#endif + diff --git a/src/foreign.c b/src/foreign.c new file mode 100644 index 0000000..dab1c21 --- /dev/null +++ b/src/foreign.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "foreign.h" + +enum fkey foreign( const char *name, size_t length) +{ + if (length == 3) { /* UID */ + if (name[1]=='I' && name[2]=='D') + switch (name[0]) { +#if _FOREIGN_HAS_(UID) + case 'G': /* GID */ return GID; +#endif +#if _FOREIGN_HAS_(GID) + case 'U': /* UID */ return UID; +#endif + default: break; + } + } + else if (length == 4) { + switch (name[0]) { +#if _FOREIGN_HAS_(HOME) + case 'H': /* HOME */ + if (name[1]=='O' && name[2]=='M' && name[3]=='E') + return HOME; + break; +#endif +#if _FOREIGN_HAS_(EUID) + case 'E': /* EUID */ + if (name[1]=='U' && name[2]=='I' && name[3]=='D') + return EUID; + break; +#endif +#if _FOREIGN_HAS_(USER) + case 'U': /* USER */ + if (name[1]=='S' && name[2]=='E' && name[3]=='R') + return USER; + break; +#endif + default: break; + } + } + else if (length == 5 && name[0]=='E') { + switch (name[1]) { +#if _FOREIGN_HAS_(EHOME) + case 'H': /* EHOME */ + if (name[2]=='O' && name[3]=='M' && name[4]=='E') + return EHOME; + break; +#endif +#if _FOREIGN_HAS_(EUSER) + case 'U': /* EUSER */ + if (name[2]=='S' && name[3]=='E' && name[4]=='R') + return EUSER; + break; +#endif + default: break; + } + } + return _FOREIGN_INVALID_; +} + diff --git a/src/foreign.h b/src/foreign.h new file mode 100644 index 0000000..3c9066f --- /dev/null +++ b/src/foreign.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef FOREIGN_H +#define FOREIGN_H + +#define _FOREIGN_MASK_HOME_ 1 +#define _FOREIGN_MASK_UID_ 2 +#define _FOREIGN_MASK_USER_ 4 +#define _FOREIGN_MASK_GID_ 8 +#define _FOREIGN_MASK_EHOME_ 16 +#define _FOREIGN_MASK_EUID_ 32 +#define _FOREIGN_MASK_EUSER_ 64 + +#define _FOREIGNS_TO_USE_ ( _FOREIGN_MASK_HOME_ \ + | _FOREIGN_MASK_USER_ ) + +#define _FOREIGN_HAS_(x) (0 != ((_FOREIGNS_TO_USE_) & (_FOREIGN_MASK_##x##_))) + +enum fkey { + _FOREIGN_INVALID_ = -1, +#if _FOREIGN_HAS_(HOME) + HOME, +#endif +#if _FOREIGN_HAS_(UID) + UID, +#endif +#if _FOREIGN_HAS_(USER) + USER, +#endif +#if _FOREIGN_HAS_(GID) + GID, +#endif +#if _FOREIGN_HAS_(EHOME) + EHOME, +#endif +#if _FOREIGN_HAS_(EUID) + EUID, +#endif +#if _FOREIGN_HAS_(EUSER) + EUSER, +#endif + _FOREIGN_COUNT_ +}; + +/* get the foreign key for the 'name' of 'length' */ +enum fkey foreign( const char *name, size_t length); + +#endif + diff --git a/src/heap.c b/src/heap.c new file mode 100644 index 0000000..6bca486 --- /dev/null +++ b/src/heap.c @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include + +#include "heap.h" + +/* align to a size_t size */ +inline static size_t align(size_t size) +{ + /* we assume that sizeof(size_t) is a power of 2 */ + assert( (sizeof(size_t) & (sizeof(size_t)-1)) == 0 ); + return (size + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1); +} + +/* align to a page size */ +inline static size_t pagealign( size_t size) +{ + /* we assume that pagesize is a power of 2 */ + size_t pagemask = (size_t)sysconf(_SC_PAGE_SIZE) - 1; + assert( (pagemask & (pagemask+1)) == 0 ); + return (size + pagemask) & ~pagemask; +} + +int heap_create( struct heap *heap, size_t capacity) +{ + char *data; + + /* allocation of the heap */ + capacity = pagealign(capacity ? capacity : 1); + data = mmap(NULL, capacity, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + + /* success of the allocation? */ + if (data == MAP_FAILED) + return -1; + + /* yes. initialisae the heap */ + heap->data = data; + heap->capacity = capacity; + heap->size = 0; + return 0; +} + +int heap_destroy( struct heap *heap) +{ + return munmap( heap->data, heap->capacity); +} + +int heap_resize( struct heap *heap, size_t size) +{ + /* has heap enought data? */ + if (size > heap->capacity) { + + /* no. resizing of the heap. */ + /* compute the sizes and realloc */ + size_t capa = pagealign(size); + char *data = mremap(heap->data, heap->capacity, capa, MREMAP_MAYMOVE); + + /* error if failure */ + if (data == MAP_FAILED) + return -1; + + /* record new parameters. */ + heap->data = data; + heap->capacity = capa; + } + heap->size = size; + + return 0; +} + +size_t heap_alloc( struct heap *heap, size_t count) +{ + size_t result = heap->size; + if( heap_resize( heap, align( result + count)) != 0) + result = HNULL; + return result; +} + +size_t heap_strndup( struct heap *heap, const char *value, size_t length) +{ + size_t offset; + char *string; + + offset = heap_alloc( heap, length+1); + if (offset != HNULL) { + string = heap_address( heap, offset); + memcpy( string, value, length); + string[length] = 0; + } + return offset; +} + +size_t heap_strdup( struct heap *heap, const char *string) +{ + return heap_strndup( heap, string, strlen(string)); +} + +int heap_read_write( struct heap *heap) +{ + return mprotect(heap->data, heap->capacity, PROT_READ|PROT_WRITE); +} + +int heap_read_only( struct heap *heap) +{ + return mprotect(heap->data, heap->capacity, PROT_READ); +} + + + diff --git a/src/heap.h b/src/heap.h new file mode 100644 index 0000000..d9713e5 --- /dev/null +++ b/src/heap.h @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef HEAP_H +#define HEAP_H + +/* null offset value */ +#define HNULL ((size_t)-1) + +/* structure defining the heap */ +struct heap { + char *data; /* pointer to data of the heap */ + size_t size; /* count of used byte of the heap */ + size_t capacity; /* count of byte of the heap */ +}; + +/* + Return the address of the 'heap' memory of 'offset'. + The returned pointer is only valid until + a call to 'heap_destroy', 'heap_resize', 'heap_alloc' +*/ +inline static void *heap_address( struct heap *heap, size_t offset) +{ + return heap->data + offset; +} + + +/* + Initialize the 'heap' with a 'capacity' in byte. + The allocated size will be zero. + Returns 0 if success, -1 if error occured (see then errno) +*/ +int heap_create( struct heap *heap, size_t capacity); + +/* + Resize the 'heap' to 'size'. + Returns 0 if success, -1 if error occured (see then errno) +*/ +int heap_resize( struct heap *heap, size_t size); + +/* + Allocation of 'count' bytes from the 'heap'. + Returns the offset (in byte) from the start + of the heap. Return HNULL in case of error (see then errno). +*/ +size_t heap_alloc( struct heap *heap, size_t count); + +/* + Copy the 'string' of 'length' in the 'heap', appending a terminating null. + Return the offset or HNULL if error. +*/ +size_t heap_strndup( struct heap *heap, const char *string, size_t length); + +/* + Copy the 'string' in the 'heap' with the terminating null. + Return the offset or HNULL if error. +*/ +size_t heap_strdup( struct heap *heap, const char *string); + +/* + Destroy the 'heap'. + Returns 0 if success, -1 if error occured (see then errno) +*/ +int heap_destroy( struct heap *heap); + +/* + Set the heap as read/write + Returns 0 if success, -1 if error occured (see then errno) +*/ +int heap_read_write( struct heap *heap); + +/* + Set the heap as read only + Returns 0 if success, -1 if error occured (see then errno) +*/ +int heap_read_only( struct heap *heap); + +#endif + diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..0b95916 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,319 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include + +#include "parser.h" + +#ifndef MINIMUM_DATA_SIZE +# define MINIMUM_DATA_SIZE 128 +#endif +#ifndef DEFAULT_MAXIMUM_DATA_SIZE +# define DEFAULT_MAXIMUM_DATA_SIZE 32768 +#endif + +#ifndef _ +# define _(x) x +#endif + +int parse_utf8_config( struct parsing *parsing) +{ + char c, q, acc, overflow, escape; + char *bdata; + const char *value, *head, *end, *skey, *svar, *bvar; + size_t lkey, lvar, ldata, datasz; + int errors; + +#define iskeybeg(x) (isalpha(x)||(x)=='_') +#define iskey(x) (isalnum(x)||(x)=='_') +#define atend ((head-end) >= 0) + +#define pos(x) ((size_t)((x)-parsing->buffer)) +#define error(p,x) do{ errors++; \ + if(parsing->error \ + && !parsing->error(parsing,pos(p),x)) \ + goto stop; \ + }while(0) + + /* alloc data buffer */ + datasz = parsing->maximum_data_size; + if (datasz == 0) + datasz = DEFAULT_MAXIMUM_DATA_SIZE; + else if (datasz < MINIMUM_DATA_SIZE) + datasz = MINIMUM_DATA_SIZE; + bdata = alloca(datasz); + + /* init */ + escape = parsing->should_escape ? 1 : 0; + errors = 0; + head = parsing->buffer; + end = head + parsing->length; + goto initial; + +next_initial: + + head++; + +initial: /* expecting key, comment or ??? */ + + if (atend) + goto end_ok; + + c = *head; + + if (iskeybeg(c)) { + skey = head; + goto key; + } + + if (c == '#') + goto comment; + + if (!isspace(c)) + error(head,_("unexpected character while looking to a key start")); + + goto next_initial; + +comment: /* skipping a comment */ + + head++; + if (atend) + goto end_ok; + c = *head; + if (c == '\n') + goto next_initial; + goto comment; + +key: /* reading a key */ + + head++; + if (atend) { + error(head,_("end in a key")); + goto stop; + } + c = *head; + if (iskey(c)) + goto key; + if (c != '=') { + error(head,_("unexpected character while looking to =")); + goto next_initial; + } + lkey = head - skey; + overflow = ldata = 0; + q = 0; + goto next_value; + +escapable: + + if (escape && ldata < datasz) + bdata[ldata++] = '\\'; + +add_value: /* add the current char to data */ + + if (ldata < datasz) + bdata[ldata++] = c; + else + overflow = 1; + +next_value: + + head++; + +value: /* reading the value */ + + if (atend) { + /* end in the value */ + if (!q) + goto end_of_value; + + error(head,_("end in quoted value")); + goto stop; + } + + c = *head; + switch (c) { + + case '\\': + if (q == '\'') + goto escapable; + ++head; + if (atend) { + error(head,_("end after escape char")); + goto stop; + } + c = *head; + goto escapable; + + case '\'': + if (q == '"') + goto escapable; + q = q ^ c; + goto next_value; + + case '"': + if (q == '\'') + goto escapable; + q = q ^ c; + goto next_value; + + case '$': + if (q == '\'') + goto escapable; + + /* begin of a variable, just after $ */ + bvar = head++; + if (atend) { + error(head,_("end after $")); + goto stop; + } + c = *head; + acc = c; + if (c == '{') { + ++head; + if (atend) { + error(head,_("end after ${")); + goto stop; + } + c = *head; + } + if (!iskeybeg(c)) { + error(head,_("invalid character after $ or ${")); + goto value; + } + svar = head; + goto variable; + + default: + if (q || !isspace(c)) + goto add_value; + + goto end_of_value; + } + +end_of_value: /* end of the value */ + + if (overflow) + error(head,_("value too big")); + else if (parsing->put) + parsing->put( parsing, skey, lkey, bdata, ldata, pos(skey), pos(head)); + if (atend) + goto initial; + goto next_initial; + +variable: /* read a variable */ + + ++head; + if (atend) { + if (acc == '{') + error(head,_("unmatched pair { }")); + lvar = head - svar; + } + else { + c = *head; + if (iskey(c)) + goto variable; + lvar = head - svar; + if (acc == '{') { + if (c == '}') + ++head; + else + error(head,_("unmatched pair { }")); + } + } + if (parsing->get) { + value = parsing->get( parsing, svar,lvar,pos(bvar),pos(head)); + if (value == NULL) + error(bvar,_("no value for the variable")); + else { + while (*value) { + if (ldata < datasz) + bdata[ldata++] = *value++; + else { + overflow = 1; + break; + } + } + } + } + goto value; + +stop: +end_ok: + return -errors; + +#undef error +#undef pos +#undef atend +#undef iskey +#undef iskeybeg +} + +void parse_utf8_info( + struct parsing *parsing, + struct parsinfo *info, + size_t pos +) +{ + const char *buf, *begin, *end; + size_t length; + int lino, colno; + + /* init */ + lino = 1, colno = 1; + buf = begin = end = parsing->buffer; + length = parsing->length; + if (length < pos) + pos = length; + + /* search the begin of the line */ + while ((end - buf) != pos) { + /* dealing utf8 */ + colno += ((*end & '\xc0') != '\x80'); + /* dealing with lines */ + if(*end++ == '\n') { + begin = end; + lino++; + colno = 1; + } + } + + /* search the end of the line */ + while ((end - buf) < length && *end != '\n') + end++; + + /* record computed values */ + info->begin = begin; + info->end = end; + info->length = end - begin; + info->lino = lino; + info->colno = colno; +} + diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 0000000..d8390f4 --- /dev/null +++ b/src/parser.h @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef TIZEN_PLATFORM_WRAPPER_PARSER_H +#define TIZEN_PLATFORM_WRAPPER_PARSER_H + +/* structure used for parsing config files */ +struct parsing { + + /* The buffer to parse. */ + const char *buffer; + + /* The length of the buffer to parse. */ + size_t length; + + /* The maximum data size allowed */ + size_t maximum_data_size; + + /* Some user data, please use it as you wish. */ + void * data; + + /* Should escape the key values (inserting \ where needed) */ + int should_escape; + + /* + Callback function to resolve the variables. + Should return the value of the variable of 'key' that + length is 'length' without terminating zero. + */ + const char *(*get)( struct parsing *parsing, + const char *key, size_t length, + size_t begin_pos, size_t end_pos); + + /* + Callback function to receive new values. + Should add/insert/replace the key/value pair + given. This values aren't zero terminated. + The given length is the one without terminating nul. + */ + int (*put)( struct parsing *parsing, + const char *key, size_t key_length, + const char *value, size_t value_length, + size_t begin_pos, size_t end_pos); + + /* + Callback function to report errors. + 'buffer' is the scanned buffer. + 'position' is the position of the character raising the error. + 'message' is a short explanation of the error. + Should return 0 to stop parsing; + */ + int (*error)( struct parsing *parsing, + size_t position, const char *message); +}; + +/* + Parse the config file using data of 'parsing'. + Return 0 if not error found or a negative number + corresponding to the opposite of the count of found errors + (ex: -5 means 5 errors). + Note: works on utf8 data. +*/ +int parse_utf8_config( + struct parsing *parsing +); + +/* Structure for getting information about a position. */ +struct parsinfo { + const char *begin; /* pointer to the first char of the line */ + const char *end; /* pointer to the char just after the end of the line */ + size_t length; /* length of the line (== end-begin) */ + int lino; /* number of the line within the buffer */ + int colno; /* number of the column within the line */ +}; + +/* + Fill the data of 'info' for the current position 'pos' that is an offset + into the buffer of 'parsing'. + This function computes into info the pointers of the line containig the + char of offset 'pos', the number of this line and the column number of + the position within the line. + Note: works on utf8 data. +*/ +void parse_utf8_info( + struct parsing *parsing, + struct parsinfo *info, + size_t pos +); + + +#endif + diff --git a/src/passwd.c b/src/passwd.c new file mode 100644 index 0000000..20ae2c6 --- /dev/null +++ b/src/passwd.c @@ -0,0 +1,280 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include + +#include "heap.h" +#include "passwd.h" + +#ifndef NOT_PASSWD_ONLY + +#include "buffer.h" + +/* index of fields */ +enum { iname, ipasswd, iuid, igid, icmt, idir, ishell }; + +static const char pwfile[] = "/etc/passwd"; +static struct buffer buffer; +static size_t pos, lengths[7]; +static const char *starts[7]; + +#define is(s,i) (!strncmp(s,starts[i],lengths[i]) && !s[lengths[i]]) + +/* open the passwd file */ +static int oppw() +{ + pos = 0; + return buffer_create( &buffer, pwfile); +} + +/* close the passwd file */ +static void clpw() +{ + buffer_destroy( &buffer); +} + +/* read the passwd file */ +static int rdpw() +{ + int col = 0; + const char *head = buffer.buffer + pos; + const char *end = buffer.buffer + buffer.length; + while (head != end) { + starts[col] = head; + while(head!=end && *head!=':' && *head!='\n') head++; + lengths[col] = head - starts[col]; + col++; + if (col == 7) { + if (head==end) { + pos = head - buffer.buffer; + return 1; + } + if (*head=='\n') { + head++; + pos = head - buffer.buffer; + return 1; + } + while (head!=end && *head!='\n') head++; + if (head!=end) head++; + col = 0; + } + else { + if (head != end) { + if (*head++=='\n') + col = 0; + } + } + } + pos = head - buffer.buffer; + return 0; +} + +int pw_get( struct heap *heap, struct pwget **items) +{ + size_t user, home; + int result; + int i, n, s; + + for( n = 0 ; items[n] != NULL ; n++ ) + items[n]->set = 0; + + result = oppw(); + if (result == 0) { + s = n; + while (s && rdpw()) { + user = home = HNULL; + for( i = 0 ; i < n ; i++ ) { + if (!items[i]->set && is(items[i]->id,iuid)) { + if (user==HNULL) { + user = heap_strndup( heap, + starts[iname], lengths[iname]); + home = heap_strndup( heap, + starts[idir], lengths[idir]); + } + items[i]->set = 1; + items[i]->user = user; + items[i]->home = home; + s--; + } + } + } + clpw(); + } + return result; +} + +int pw_get_uid( const char *name, uid_t *uid) +{ + int result = oppw(); + if (result == 0) { + while (rdpw()) { + if (is(name,iname)) { + *uid = (uid_t)atoi(starts[iuid]); + clpw(); + return 0; + } + } + clpw(); + result = -1; + errno = EEXIST; + } + return result; +} + +int pw_get_gid( const char *name, gid_t *gid) +{ + int result = oppw(); + if (result == 0) { + while (rdpw()) { + if (is(name,iname)) { + *gid = (gid_t)atoi(starts[igid]); + clpw(); + return 0; + } + } + clpw(); + result = -1; + errno = EEXIST; + } + return result; +} + +#else + +#include + +#define BUFSIZE 4096 + +int pw_get( struct heap *heap, struct pwget **items) +{ + char buffer[BUFSIZE]; + struct passwd entry, *pe; + int result; + int n; + uid_t id; + + for( n = 0 ; items[n] != NULL ; n++ ) { + id = (uid_t)atoi(items[n]->id); + result = getpwuid_r( id, &entry, buffer, sizeof buffer, &pe); + if (result != 0) { + while(items[n] != NULL) items[n++]->set = 0; + return result; + } + if (pe == NULL) + items[n]->set = 0; + else { + items[n]->set = 1; + items[n]->user = heap_strdup( heap, pe->pw_name); + items[n]->home = heap_strdup( heap, pe->pw_dir); + } + } + return 0; +} + +int pw_get_uid( const char *name, uid_t *uid) +{ + char buffer[BUFSIZE]; + struct passwd entry, *pe; + int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe); + if (result == 0) { + if (pe == NULL) { + errno = EEXIST; + result = -1; + } + else { + *uid = entry.pw_uid; + } + } + return result; +} + +int pw_get_gid( const char *name, gid_t *gid) +{ + char buffer[BUFSIZE]; + struct passwd entry, *pe; + int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe); + if (result == 0) { + if (pe == NULL) { + errno = EEXIST; + result = -1; + } + else { + *gid = entry.pw_gid; + } + } + return result; +} + +#endif + +#ifdef TEST_PASSWD +#include +int main(int argc,char**argv) { + struct heap heap; + enum { uid, gid, names } action = uid; + heap_create(&heap,1000); + while(*++argv) { + if (!strcmp(*argv,"-u")) + action = uid; + else if (!strcmp(*argv,"-g")) + action = gid; + else if (!strcmp(*argv,"-n")) + action = names; + else if (action == uid) { + uid_t u = 0; + int sts = pw_get_uid( *argv, &u); + printf("uid(%s)=%d [%d]\n",*argv,(int)u,sts); + } + else if (action == gid) { + gid_t g = 0; + int sts = pw_get_gid( *argv, &g); + printf("gid(%s)=%d [%d]\n",*argv,(int)g,sts); + } + else if (action == names) { + struct pwget pw = { .id=*argv }, *ppw[2] = { &pw, NULL }; + int sts = pw_get( &heap, ppw); + printf("names(%s) set=%d",*argv,pw.set); + if (pw.set) { + printf(" name=%s home=%s", + pw.user==HNULL ? "(null)" + : (char*)heap_address( &heap, pw.user), + pw.home==HNULL ? "(null)" + : (char*)heap_address( &heap, pw.home)); + } + printf(" [%d]\n",sts); + } + } + return 0; +} +#endif + + diff --git a/src/passwd.h b/src/passwd.h new file mode 100644 index 0000000..23352fe --- /dev/null +++ b/src/passwd.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef TIZEN_PLATFORM_WRAPPER_PASSWD_H +#define TIZEN_PLATFORM_WRAPPER_PASSWD_H + +struct pwget { + int set; + const char *id; + size_t user; + size_t home; +}; + +int pw_get( struct heap *heap, struct pwget **items); +int pw_get_uid( const char *name, uid_t *uid); +int pw_get_gid( const char *name, gid_t *gid); + +#endif + diff --git a/src/scratch.c b/src/scratch.c new file mode 100644 index 0000000..dc3c918 --- /dev/null +++ b/src/scratch.c @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#ifndef INITIAL_SCRATCH_CAPACITY +#define INITIAL_SCRATCH_CAPACITY 240 +#endif + +#if INITIAL_SCRATCH_CAPACITY <= 0 +#error "bad value for INITIAL_SCRATCH_CAPACITY" +#endif + +#ifndef NOT_MULTI_THREAD_SAFE +#include +static pthread_key_t tlskey; +static int key_initialized = 0; +#else +static void *global_scratch = NULL; +#endif + +/* CAUTION: in a multitheaded context, it is expected that +=========== the function scratchcat is call under a mutex. +If it is not the case please check for initializing 'tlskey' +only one time before use of it. */ + +const char *scratchcat( int ispath, const char **strings) +{ + void *scratch, *p; + char *result; + size_t length, capacity; + const char *instr; + char c, pc; + + /* get the recorded pointer on scrtch area */ +#ifndef NOT_MULTI_THREAD_SAFE + if (!key_initialized) { + key_initialized = 1; + pthread_key_create( &tlskey, (void(*)(void*))free); + } + scratch = pthread_getspecific( tlskey); +#else + scratch = global_scratch; +#endif + + /* create the scratch area if needed */ + if (scratch == NULL) { + capacity = INITIAL_SCRATCH_CAPACITY; + p = malloc( capacity + sizeof(size_t)); + if (p == NULL) + return NULL; + *((size_t*)p) = capacity; + scratch = p; +#ifndef NOT_MULTI_THREAD_SAFE + pthread_setspecific( tlskey, p); +#else + global_scratch = p; +#endif + } + + /* set local data for scratch area */ + capacity = *((size_t*)scratch); + result = (char*)(1+((size_t*)scratch)); + length = 0; + + /* copy the strings */ + c = 0; + pc = 1; + while(pc) { + + if (c == 0) { + instr = *strings++; + if (instr != NULL) { + c = *instr; + if (c == 0) + continue; + if (!ispath) + instr++; + else if(c != '/' && pc != '/') + c = '/'; + else if(c == '/' && pc == '/') { + instr++; + continue; + } + else + instr++; + } + } + else { + c = *instr; + if (c == 0) + continue; + instr++; + } + + /* extend the scratch area if needed */ + if (length == capacity) { + capacity = 2 * capacity; + p = realloc( scratch, capacity + sizeof(size_t)); + if (p == NULL) + return NULL; + *((size_t*)p) = capacity; + if (p != scratch) { + scratch = p; +#ifndef NOT_MULTI_THREAD_SAFE + pthread_setspecific( tlskey, p); +#else + global_scratch = p; +#endif + result = (char*)(1+((size_t*)p)); + } + } + + /* append the char */ + pc = result[length++] = c; + } + + return result; +} + + +#ifdef TEST_SCRATCH +#include +int main(int argc, const char**argv) { + int ispath; + argv++; + ispath = argv[0] && argv[0][0] == '-' && argv[0][1] == 'p'; + printf("%s\n",scratchcat(ispath,argv+ispath)); + return 0; +} +#endif diff --git a/src/scratch.h b/src/scratch.h new file mode 100644 index 0000000..c571348 --- /dev/null +++ b/src/scratch.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef TIZEN_PLATFORM_WRAPPER_SCRATCH_H +#define TIZEN_PLATFORM_WRAPPER_SCRATCH_H + +/* + Return a scratch buffer containing the concatenation of the strings of the + array 'strings'. If 'ispath' isn't zero (then is "true") the directory + separator is inserted between the strings. The last item of 'strings' must + be NULL to indicate the end. + + The returned value is a scratch buffer (unique for the thread) that is + available until the next call. + + Can return NULL in case of internal error (memory depletion). + + Example: + + char *array[] = { "hello", " ", "world", "!", NULL }; + + scratchcat( 0, array) will return "hello world!" + + scratchcat( 1, array) will return "hello/ /world/!" +*/ +const char *scratchcat( int ispath, const char **strings); + + +#endif + diff --git a/src/toolbox.c b/src/toolbox.c new file mode 100644 index 0000000..bc7911d --- /dev/null +++ b/src/toolbox.c @@ -0,0 +1,746 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include + +#include "parser.h" +#include "heap.h" +#include "buffer.h" +#include "foreign.h" + +/*======================================================================*/ + +#ifndef CONFIGPATH +# define CONFIGPATH "/etc/tizen-platform.conf" +#endif + +#ifndef TOOLNAME +# define TOOLNAME "tzplatform-tool" +#endif + +/*== TYPES =============================================================*/ + +/* for recording read keys */ +struct key { + struct key *next; /* link to next */ + const char *name; /* name of the key */ + const char *value; /* value of the key */ + size_t begin; /* positions of begin (used by pretty) */ + size_t end; /* positions of end (used by pretty) */ + int dependant; /* is dependant (used by rpm) */ +}; + +/* for recording used variables */ +struct var { + struct var *next; /* link to next */ + const char *name; /* name of the variable */ + const char *normal; /* normalized value: "${name}" (for pretty) */ + int dependant; /* is dependant (used by rpm) */ +}; + +/*== STATIC DATA =======================================================*/ + +static char help[] = "\ +\n\ +usage: "TOOLNAME" [command] [--] [file]\n\ +\n\ +You can specify the 'file' to process.\n\ +The default file is "CONFIGPATH"\n\ +Specifying - mean the standard input.\n\ +\n\ +Commands:\n\ +\n\ +help Display this help\n\ +check Check validity of 'file' (this is the default command)\n\ +pretty Pretty print of the 'file' (the normalized format)\n\ +h Produce the C header with the enumeration of the variables\n\ +c Produce the C code to hash the variable names\n\ +rpm Produce the macro file to use with RPM\n\ +\n\ +"; + +static char genh_head[] = "\ +/* I'm generated. Dont edit me! */\n\ +#ifndef TZPLATFORM_VARIABLES_H\n\ +#define TZPLATFORM_VARIABLES_H\n\ +#ifdef __cplusplus\n\ +extern \"C\" {\n\ +#endif\n\ +enum tzplatform_variable {\n\ +\t_TZPLATFORM_VARIABLES_INVALID_ = -1,\n\ +"; + +static char genh_tail[] = "\ +\t_TZPLATFORM_VARIABLES_COUNT_\n\ +};\n\ +#ifdef __cplusplus\n\ +}\n\ +#endif\n\ +#endif\n\ +"; + +static char gperf_head[] = "\ +struct varassoc {\n\ + int offset;\n\ + enum tzplatform_variable id;\n\ +};\n\ +%%\n\ +"; + +static char *gperf_command[] = { + "/bin/sh", + "-c", + "gperf -r -m 100 --null-strings -C -P -L ANSI-C -c" + " -t -N hashvar -Q varpool -K offset -G -W namassoc" + " -F \", _TZPLATFORM_VARIABLES_INVALID_\"", + NULL +}; + +static char rpm_head[] = "\ +# I'm generated. Dont edit me! \n\ +\n\ +"; + +/*== GLOBALS VARIABLES =================================================*/ + +/* name of the meta file to process */ +static const char * metafilepath = CONFIGPATH; + +/* list of the read keys */ +static struct key *keys = NULL; + +/* list of the used variables */ +static struct var *vars = NULL; + +/* count of errors */ +static int errcount = 0; + +/* dependency state */ +static int dependant = 0; + +/* action to perform */ +static enum { CHECK, PRETTY, GENC, GENH, RPM } action = CHECK; + +/* output of error */ +static int notstderr = 0; + +/*======================================================================*/ + +/* write the error message */ +static void vwriterror( const char *format, va_list ap) +{ + vfprintf(notstderr ? stdout : stderr, format, ap); +} + +/* write the error message */ +static void writerror( const char *format, ...) +{ + va_list ap; + va_start(ap, format); + vwriterror( format, ap); + va_end(ap); +} + +/* write error and exit */ +static void fatal( const char *format, ...) +{ + va_list ap; + + writerror("Error, "); + va_start(ap, format); + vwriterror( format, ap); + va_end(ap); + writerror(".\n"); + + /* exit */ + exit(1); +} + +/* error in the command line */ +static void argerror( const char *format, ...) +{ + va_list ap; + + writerror("Error, "); + va_start(ap, format); + vwriterror( format, ap); + va_end(ap); + writerror(".\nType '"TOOLNAME" help' to get usage.\n"); + + /* exit */ + exit(1); +} + +/*== MANAGEMENT OF THE LIST OF READ KEYS ===============================*/ + +/* search a key of 'name' and length 'lname' and return it or NULL */ +static struct key *key_search( const char *name, size_t lname) +{ + struct key *result = keys; + + while(result!=NULL && (strncmp( result->name, name, lname)!=0 + || result->name[lname]!=0)) + result = result->next; + + return result; +} + +/* append a new key to the list and return it or NULL if allocations failed */ +static struct key *key_add( const char *name, size_t lname, + const char *value, size_t lvalue, + size_t begin_pos, size_t end_pos) +{ + struct key *result, *prev; + char *sname, *svalue; + + /* allocations */ + result = malloc(sizeof *result); + sname = strndup( name, lname); + svalue = strndup( value, lvalue); + if (result == NULL || sname == NULL || svalue == NULL) { + /* failure of allocations */ + free( result); + free( sname); + free( svalue); + result = NULL; + } + else { + /* success: init the structure */ + result->next = NULL; + result->name = sname; + result->value = svalue; + result->begin = begin_pos; + result->end = end_pos; + result->dependant = dependant; + + /* link at end of the list */ + prev = keys; + if (prev == NULL) + keys = result; + else { + while(prev!=NULL && prev->next!=NULL) + prev = prev->next; + prev->next = result; + } + } + + return result; +} + +/*======================================================================*/ + +/* search a var of 'name' and length 'lname' and return it or NULL */ +static struct var *var_search( const char *name, size_t lname) +{ + struct var *result = vars; + while(result!=NULL && (strncmp( result->name, name, lname)!=0 + || result->name[lname]!=0)) + result = result->next; + + return result; +} + +/* append a new var to the list and return it or NULL if allocations failed */ +static struct var *var_add( const char *name, size_t lname, + int depend, const char *value) +{ + struct var *result, *prev; + char *sname, *normal; + size_t length; + + /* check for the value */ + if (action == RPM && value != NULL) { + length = strlen( value) + 1; + } + else { + value = NULL; + length = lname + 4; + } + /* allocations */ + result = malloc(sizeof *result); + sname = strndup( name, lname); + normal = malloc( length); + if (result == NULL || sname == NULL || normal == NULL) { + /* failure of allocations */ + free( result); + free( sname); + free( normal); + result = NULL; + } + else { + /* success: init the structure */ + result->next = NULL; + result->name = sname; + result->normal = normal; + result->dependant = depend; + if (value) { + memcpy( normal, value, length); + } + else { + *normal++ = '$'; + *normal++ = '{'; + memcpy( normal, name, lname); + normal += lname; + *normal++ = '}'; + *normal = 0; + } + + /* link at end of the list */ + prev = vars; + if (prev == NULL) + vars = result; + else { + while(prev!=NULL && prev->next!=NULL) + prev = prev->next; + prev->next = result; + } + } + + return result; +} + +/*== CALLBACK OF PARSING OF THE META FILE ==============================*/ + +static void conferr( const char *format, ...) +{ + va_list ap; + + notstderr = action == CHECK; + writerror( "ERROR "); + va_start(ap, format); + vwriterror( format, ap); + va_end(ap); + notstderr = 0; +} + +static int errcb( struct parsing *parsing, + size_t pos, const char *message) +{ + struct parsinfo info; + + /* get the info */ + parse_utf8_info( parsing, &info, pos); + + /* emit the error */ + conferr("line %d: %s\n..: %.*s\n..: %*s\n", + info.lino, message, + (int)info.length, info.begin, + info.colno, "^"); + + /* count it */ + errcount++; + + /* continue to parse */ + return 1; +} + +static int putcb( struct parsing *parsing, + const char *name, size_t lname, + const char *value, size_t lvalue, + size_t begin_pos, size_t end_pos) +{ + enum fkey fkey; + struct key *key; + struct parsinfo here, there; + + /* check that it is not a foreign key */ + fkey = foreign( name, lname); + if (fkey != _FOREIGN_INVALID_) { + parse_utf8_info( parsing, &here, begin_pos); + conferr("line %d: reserved variable name '%.*s'\n" + "..: %.*s\n..: %*s\n", + here.lino, (int)lname, name, + here.lino, (int)here.length, here.begin, here.colno, "^"); + + errcount++; + dependant = 0; + return 0; + } + + /* search if already defined */ + key = key_search( name, lname); + if (key != NULL) { + + /* yes! that's an error */ + parse_utf8_info( parsing, &here, begin_pos); + parse_utf8_info( parsing, &there, key->begin); + conferr("line %d: redefinition of '%s'\n" + "...was defined line %d\n..: %.*s\n..: %*s\n" + "...is redefined line %d\n..: %.*s\n..: %*s\n", + here.lino, key->name, + there.lino, (int)there.length, there.begin, there.colno, "^", + here.lino, (int)here.length, here.begin, here.colno, "^"); + + errcount++; + dependant = 0; + return 0; + } + + /* create and record the key */ + key = key_add( name, lname, value, lvalue, begin_pos, end_pos); + dependant = 0; + if (key != NULL) + return 0; + + /* no can't because of memory! */ + fatal("out of memory"); + errcount++; + return -1; +} + +static const char *getcb( struct parsing *parsing, + const char *name, size_t length, + size_t begin_pos, size_t end_pos) +{ + struct parsinfo here; + struct var *var; + enum fkey fkey; + struct key *key; + int depend; + + /* search if already defined */ + var = var_search( name, length); + if (var != NULL) { + /* yes cool, return the normalized form */ + if (var->dependant) + dependant = 1; + return var->normal; + } + + /* search the variable */ + fkey = foreign( name, length); + key = key_search( name, length); + + if (fkey == _FOREIGN_INVALID_ && key == NULL) { + + /* not a valid variable, emit the error */ + parse_utf8_info( parsing, &here, begin_pos); + conferr("line %d: use of unknown variable '%.*s'\n" + "..: %.*s\n..: %*s\n", + here.lino, (int)length, name, + (int)here.length, here.begin, here.colno, "^"); + errcount++; + dependant = 1; /* kind of invalidity */ + return "***error***"; /* avoid further error */ + } + + /* valid variables: those of foreign or the already defined keys */ + + /* set dependant state */ + depend = fkey != _FOREIGN_INVALID_ || key->dependant; + if (depend) + dependant = 1; + + /* create and record the variable */ + var = var_add( name, length, depend, key==NULL ? NULL : key->value); + if (var != NULL) + /* created, return the normalized form */ + return var->normal; + + /* memory depletion */ + fatal("out of memory"); + dependant = 1; /* kind of invalidity */ + errcount++; + return "***error***"; /* avoid further error */ +} + +/*======================================================================*/ + +/* pretty print the read file */ +static int pretty( const char *buffer, size_t length, FILE *output) +{ + int status; + struct key *key = keys; + size_t pos = 0; + + while (pos < length && key != NULL) { + if (pos < key->begin) { + status = fprintf(output, "%.*s", (int)(key->begin-pos), buffer+pos); + if (status < 0) + return status; + } + status = fprintf( output, "%s=%s", key->name, key->value); + if (status < 0) + return status; + pos = key->end; + key = key->next; + } + + if (pos < length) { + status = fprintf( output, "%.*s", (int)(length-pos), buffer+pos); + if (status < 0) + return status; + } + + return 0; +} + +/* generate the header */ +static int genh( FILE *output) +{ + struct key *key; + int status; + + status = fprintf( output, "%s", genh_head); + if (status < 0) + return status; + for (key = keys ; key != NULL ; key = key->next) { + status = fprintf( output, "\t%s,\n", key->name); + if (status < 0) + return status; + } + status = fprintf( output, "%s", genh_tail); + if (status < 0) + return status; + return 0; +} + +/* generate hash code using gperf */ +static int genc(FILE *output) +{ + struct key *key; + int fds[2]; + pid_t pid; + int result, sts; + size_t l; + + result = pipe(fds); + if (result != 0) + return result; + + fflush( output); + pid = fork(); + if (pid == -1) { + close( fds[0]); + close( fds[1]); + fatal( "can't fork"); + result = -1; + } + else if (pid == 0) { + dup2( fds[0], 0); + close( fds[0]); + close( fds[1]); + if (fileno(output) != 1) + dup2( fileno(output), 1); + result = execve( gperf_command[0], gperf_command, environ); + fatal("can't execute gperf"); + exit(1); + } + else { + close( fds[0]); + sts = write( fds[1], gperf_head, sizeof(gperf_head)-1); + if (sts < 0) + result = sts; + for (key = keys ; key != NULL ; key = key->next) { + l = strlen( key->name); + sts = write( fds[1], key->name, l); + if (sts < 0) + result = sts; + sts = write( fds[1], ", ", 2); + if (sts < 0) + result = sts; + sts = write( fds[1], key->name, l); + if (sts < 0) + result = sts; + sts = write( fds[1], "\n", 1); + if (sts < 0) + result = sts; + } + close( fds[1]); + wait(&result); + sts = WIFEXITED(result) && WEXITSTATUS(result)==0 ? 0 : -1; + if (sts < 0) + result = sts; + } + return result; +} + +/* generate the rpm macros */ +static int rpm( FILE *output) +{ + struct key *key; + int status; + + status = fprintf( output, "%s", rpm_head); + if (status < 0) + return status; + for (key = keys ; key != NULL ; key = key->next) { + if (!key->dependant) { + status = fprintf( output, "%%%-40s %s\n", key->name, key->value); + if (status < 0) + return status; + } + } + return 0; +} + +/* main of processing */ +static int process() +{ + struct parsing parsing; + struct buffer buffer; + int result; + + /* read the file */ + result = buffer_create( &buffer, metafilepath); + if (result != 0) { + fatal( "can't read file %s", metafilepath); + return -1; + } + + /* parse the file */ + parsing.buffer = buffer.buffer; + parsing.length = buffer.length; + parsing.maximum_data_size = 0; + parsing.should_escape = action!=RPM; + parsing.data = 0; + parsing.get = getcb; + parsing.put = putcb; + parsing.error = errcb; + dependant = 0; + result = parse_utf8_config( &parsing); + if (result != 0) { + buffer_destroy( &buffer); + fatal( "while parsing the file %s", metafilepath); + return -1; + } + if (errcount != 0) { + buffer_destroy( &buffer); + fatal( "%d errors detected %s", errcount, metafilepath); + return -1; + } + + /* process */ + switch( action) { + case CHECK: + break; + case PRETTY: + pretty( buffer.buffer, buffer.length, stdout); + break; + case GENH: + genh( stdout); + break; + case GENC: + genc( stdout); + break; + case RPM: + rpm( stdout); + break; + } + + buffer_destroy( &buffer); + return 0; +} + +/*======================================================================*/ + +/* very simple argument parsing */ +static int arguments( char **argv) +{ + /* skip the program name*/ + argv++; + + /* scan first arg */ + if (*argv == NULL) { + /* no argument then default is to check */ + action = CHECK; + } + else { + /* check if first argument is 'check', 'pretty', 'c', 'h', '-h', + '--help' or 'help' */ + if (0 == strcmp( *argv, "check")) { + action = CHECK; + argv++; + } + else if (0 == strcmp( *argv, "pretty")) { + action = PRETTY; + argv++; + } + else if (0 == strcmp( *argv, "c")) { + action = GENC; + argv++; + } + else if (0 == strcmp( *argv, "h")) { + action = GENH; + argv++; + } + else if (0 == strcmp( *argv, "rpm")) { + action = RPM; + argv++; + } + else if (0 == strcmp( *argv, "help")) { + printf("%s", help); + exit(0); + return -1; + } + else if (**argv == '-') { + if (0 == strcmp( *argv, "--") || 0 == strcmp( *argv, "-") ) { + action = CHECK; + } + else { + argerror( "unknown option '%s'", *argv); + return -1; + } + } + /* skip the -- arg if present */ + if (*argv != NULL && 0 == strcmp( *argv, "--")) { + argv++; + } + /* get a meta file argument */ + if (*argv != NULL) { + if (0 == strcmp( *argv, "-")) + metafilepath = "/dev/stdin"; + else + metafilepath = *argv; + argv++; + } + /* check that there is no extra argument */ + if (*argv != NULL) { + argerror("extra argument found '%s'",*argv); + return -1; + } + } + return 0; +} + +int main(int argc, char **argv) +{ + if (arguments(argv) == 0) + if (process() == 0) + return 0; + + return 1; +} + + diff --git a/src/tzplatform_config.c b/src/tzplatform_config.c new file mode 100644 index 0000000..da7fbe4 --- /dev/null +++ b/src/tzplatform_config.c @@ -0,0 +1,599 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef NOT_MULTI_THREAD_SAFE +#include +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +static inline void lock() { pthread_mutex_lock(&mutex); } +static inline void unlock() { pthread_mutex_unlock(&mutex); } +#else +static inline void lock() { } +static inline void unlock() { } +#endif + +#ifndef CONFIGPATH +#define CONFIGPATH "/etc/tizen-platform.conf" +#endif + +#include "tzplatform_variables.h" +#include "tzplatform_config.h" +#include "parser.h" +#include "heap.h" +#include "buffer.h" +#include "foreign.h" +#include "scratch.h" +#include "passwd.h" + +#define _HAS_IDS_ ( _FOREIGN_HAS_(UID) \ + || _FOREIGN_HAS_(EUID) \ + || _FOREIGN_HAS_(GID) ) + +#define _HAS_PWS_ ( _FOREIGN_HAS_(HOME) \ + || _FOREIGN_HAS_(USER) \ + || _FOREIGN_HAS_(EHOME) \ + || _FOREIGN_HAS_(EUSER) ) + +/* structure for reading config files */ +struct reading { + int errcount; + size_t dynvars[_FOREIGN_COUNT_]; + size_t offsets[_TZPLATFORM_VARIABLES_COUNT_]; +}; + +/* local and static variables */ +static const char metafilepath[] = CONFIGPATH; +static const char emptystring[] = ""; +static struct heap heap; +static enum { RESET=0, ERROR, VALID } state = RESET; +static const char *var_values[_TZPLATFORM_VARIABLES_COUNT_]; +static const char *var_names[_TZPLATFORM_VARIABLES_COUNT_]; + +#include "hash.inc" + +/* write the error message */ +static void writerror( const char *format, ...) +{ + va_list ap; + fprintf( stderr, "tzplatform_config ERROR: "); + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + fprintf( stderr, "\n"); +} + +#if _HAS_IDS_ +/* fill the foreign variables for ids */ +static void foreignid( struct reading *reading) +{ + int n; + char buffer[50]; + +#if _FOREIGN_HAS_(UID) + /* set the uid */ + if (reading->dynvars[UID] == HNULL) { + n = snprintf( buffer, sizeof buffer, "%d", (int)getuid()); + if (0 < n && n < (int)(sizeof buffer)) + reading->dynvars[UID] = heap_strndup( &heap, buffer, (size_t)n); + } +#endif + +#if _FOREIGN_HAS_(EUID) + /* set the euid */ + if (reading->dynvars[EUID] == HNULL) { + n = snprintf( buffer, sizeof buffer, "%d", (int)geteuid()); + if (0 < n && n < (int)(sizeof buffer)) + reading->dynvars[EUID] = heap_strndup( &heap, buffer, (size_t)n); + } +#endif + +#if _FOREIGN_HAS_(GID) + /* set the gid */ + if (reading->dynvars[GID] == HNULL) { + n = snprintf( buffer, sizeof buffer, "%d", (int)getgid()); + if (0 < n && n < (int)(sizeof buffer)) + reading->dynvars[GID] = heap_strndup( &heap, buffer, (size_t)n); + } +#endif +} +#endif + +#if _HAS_PWS_ +/* fill the foreign variables for home and user */ +static void foreignpw( struct reading *reading) +{ + int n = 0; + struct pwget *array[3]; +#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER) + struct pwget uid; + char suid[50]; +#endif +#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER) + struct pwget euid; + char seuid[50]; +#endif + +#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER) + if ( +#if _FOREIGN_HAS_(HOME) + reading->dynvars[HOME] == HNULL +#endif +#if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER) + || +#endif +#if _FOREIGN_HAS_(USER) + reading->dynvars[USER] == HNULL +#endif + ) { + snprintf( suid, sizeof suid, "%u", (unsigned)getuid()); + uid.id = suid; + array[n++] = &uid; + } + else { + uid.set = 0; + } +#endif + +#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER) + if ( +#if _FOREIGN_HAS_(EHOME) + reading->dynvars[EHOME] == HNULL +#endif +#if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER) + || +#endif +#if _FOREIGN_HAS_(EUSER) + reading->dynvars[EUSER] == HNULL +#endif + ) { + snprintf( seuid, sizeof seuid, "%u", (unsigned)geteuid()); + euid.id = seuid; + array[n++] = &euid; + } + else { + euid.set = 0; + } +#endif + + if (n) { + array[n] = NULL; + if (pw_get( &heap, array) == 0) { +#if _FOREIGN_HAS_(HOME) + if (uid.set) + reading->dynvars[HOME] = uid.home; +#endif +#if _FOREIGN_HAS_(USER) + if (uid.set) + reading->dynvars[USER] = uid.user; +#endif +#if _FOREIGN_HAS_(EHOME) + if (euid.set) + reading->dynvars[EHOME] = euid.home; +#endif +#if _FOREIGN_HAS_(EUSER) + if (euid.set) + reading->dynvars[EUSER] = euid.user; +#endif + } + } +} +#endif + +/* get the foreign variable */ +static const char *foreignvar( struct reading *reading, + const char *name, size_t length) +{ + enum fkey key = foreign( name, length); + size_t offset; + + switch (key) { +#if _HAS_PWS_ +#if _FOREIGN_HAS_(HOME) + case HOME: +#endif +#if _FOREIGN_HAS_(USER) + case USER: +#endif +#if _FOREIGN_HAS_(EHOME) + case EHOME: +#endif +#if _FOREIGN_HAS_(EUSER) + case EUSER: +#endif + foreignpw( reading); + break; +#endif + +#if _HAS_IDS_ +#if _FOREIGN_HAS_(UID) + case UID: +#endif +#if _FOREIGN_HAS_(GID) + case GID: +#endif +#if _FOREIGN_HAS_(EUID) + case EUID: +#endif + foreignid( reading); + break; +#endif + + default: + return NULL; + } + offset = reading->dynvars[key]; + return offset==HNULL ? NULL : heap_address( &heap, offset); +} + +/* callback for parsing errors */ +static int errcb( struct parsing *parsing, + size_t position, const char *message) +{ + struct parsinfo info; + struct reading *reading = parsing->data; + + /* count the error */ + reading->errcount++; + + /* print the error */ + parse_utf8_info( parsing, &info, position); + writerror( "%s (file %s line %d)", message, metafilepath, info.lino); + + /* continue to parse */ + return 0; +} + +/* callback for solving variables */ +static const char *getcb( struct parsing *parsing, + const char *key, size_t length, + size_t begin_pos, size_t end_pos) +{ + struct parsinfo info; + const char *result; + const struct varassoc *vara; + size_t offset; + struct reading *reading = parsing->data; + + /* try to find a tzplatform variable */ + vara = hashvar( key, length); + if (vara) { + /* found: try to use it */ + offset = reading->offsets[(int)vara->id]; + if (offset != HNULL) + result = heap_address( &heap, offset); + else + result = NULL; + } + else { + /* that is a foreign variable */ + result = foreignvar( reading, key, length); + } + + /* emit the error and then return */ + if (result == NULL) { + reading->errcount++; + parse_utf8_info( parsing, &info, begin_pos); + writerror( "undefined value for %.*s (file %s line %d)", + length, key, metafilepath, info.lino); + result = emptystring; /* avoid error of the parser */ + } + return result; +} + +/* callback to define variables */ +static int putcb( struct parsing *parsing, + const char *key, size_t key_length, + const char *value, size_t value_length, + size_t begin_pos, size_t end_pos) +{ + struct parsinfo info; + const struct varassoc *vara; + size_t offset; + char *string; + struct reading *reading = parsing->data; + + /* try to find a tzplatform variable */ + vara = hashvar( key, key_length); + if (vara) { + /* check that the variable isn't already defined */ + offset = reading->offsets[(int)vara->id]; + if (offset != HNULL) { + reading->errcount++; + parse_utf8_info( parsing, &info, begin_pos); + writerror( "redefinition of variable %.*s (file %s line %d)", + key_length, key, metafilepath, info.lino); + } + + /* allocate the variable value */ + offset = heap_alloc( &heap, value_length+1); + if (offset == HNULL) { + /* error of allocation */ + reading->errcount++; + writerror( "out of memory"); + } + else { + /* record the variable value */ + reading->offsets[(int)vara->id] = offset; + string = heap_address( &heap, offset); + memcpy( string, value, value_length); + string[value_length] = 0; + } + } + else { + /* forbidden variable */ + parse_utf8_info( parsing, &info, begin_pos); + writerror( "forbidden variable name %.*s (file %s line %d)", + key_length, key, metafilepath, info.lino); + + } + + /* continue to parse */ + return 0; +} + +/* initialize the environment */ +static void initialize() +{ + struct buffer buffer; + struct parsing parsing; + struct reading reading; + size_t offset; + int i, result; + + /* clear the variables */ + reading.errcount = 0; + for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) { + reading.dynvars[i] = HNULL; + } + for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) { + var_values[i] = NULL; + reading.offsets[i] = HNULL; + } + + /* read the configuration file */ + result = buffer_create( &buffer, metafilepath); + if (result != 0) { + writerror( "can't read file %s",metafilepath); + state = ERROR; + return; + } + + /* create the heap */ + result = heap_create( &heap, 1); + if (result != 0) { + buffer_destroy( &buffer); + writerror( "out of memory"); + state = ERROR; + return; + } + + /* read the file */ + parsing.buffer = buffer.buffer; + parsing.length = buffer.length; + parsing.maximum_data_size = 0; + parsing.should_escape = 0; + parsing.data = &reading; + parsing.get = getcb; + parsing.put = putcb; + parsing.error = errcb; + result = parse_utf8_config( &parsing); + buffer_destroy( &buffer); + if (result != 0 || reading.errcount != 0) { + writerror( "%d errors while parsing file %s", + reading.errcount, metafilepath); + } + + /* set the variables */ + heap_read_only( &heap); + for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) { + offset = reading.offsets[i]; + if (offset != HNULL) + var_values[i] = heap_address( &heap, offset); + else + writerror( "the variable %s isn't defined in file %s", + tzplatform_getname((enum tzplatform_variable)i), metafilepath); + /* TODO undefined variable */; + } + state = VALID; +} + +static const char *get_lock(enum tzplatform_variable id) +{ + const char *result; + int offset; + + lock(); + offset = (int)id; + if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) { + /*error("invalid id"); TODO*/ + result = NULL; + } + else { + if (state == RESET) + initialize(); + result = var_values[offset]; + } + return result; +} + +/* reset the state of the environement */ +void tzplatform_reset() +{ + lock(); + if (state != RESET) { + if (state == VALID) + heap_destroy( &heap); + state = RESET; + } + unlock(); +} + +int tzplatform_getcount() +{ + return (int)_TZPLATFORM_VARIABLES_COUNT_; +} + +const char* tzplatform_getname(enum tzplatform_variable id) +{ + const struct varassoc *iter, *end; + const char *result; + int offset; + + offset = (int)id; + if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) { + /*error("invalid id"); TODO*/ + result = NULL; + } + else { + if (!var_names[0]) { + iter = namassoc; + end = iter + (sizeof namassoc / sizeof namassoc[0]); + while (iter != end) { + if (iter->offset >= 0) + var_names[(int)iter->id] = varpool + iter->offset; + iter++; + } + } + result = var_names[offset]; + } + return result; +} + +enum tzplatform_variable tzplatform_getid(const char *name) +{ + const struct varassoc *vara = hashvar( name, strlen(name)); + return vara ? vara->id : _TZPLATFORM_VARIABLES_INVALID_; +} + +const char* tzplatform_getenv(enum tzplatform_variable id) +{ + const char *result = get_lock( id); + unlock(); + return result; +} + +int tzplatform_getenv_int(enum tzplatform_variable id) +{ + const char *value = get_lock( id); + int result = value==NULL ? -1 : atoi(value); + unlock(); + return result; +} + +const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str) +{ + const char *array[3]; + const char *result = get_lock( id); + if (result != NULL) { + array[0] = result; + array[1] = str; + array[2] = NULL; + result = scratchcat( 0, array); + } + unlock(); + return result; +} + +const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path) +{ + const char *array[3]; + const char *result = get_lock( id); + if (result != NULL) { + array[0] = result; + array[1] = path; + array[2] = NULL; + result = scratchcat( 1, array); + } + unlock(); + return result; +} + +const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2) +{ + const char *array[4]; + const char *result = get_lock( id); + if (result != NULL) { + array[0] = result; + array[1] = path; + array[2] = path2; + array[3] = NULL; + result = scratchcat( 1, array); + } + unlock(); + return result; +} + +uid_t tzplatform_getuid(enum tzplatform_variable id) +{ + uid_t result = (uid_t)-1; + const char *value = get_lock( id); + if (value != NULL) { + pw_get_uid( value, &result); + } + unlock(); + return result; +} + +gid_t tzplatform_getgid(enum tzplatform_variable id) +{ + gid_t result = (uid_t)-1; + const char *value = get_lock( id); + if (value != NULL) { + pw_get_gid( value, &result); + } + unlock(); + return result; +} + +#ifdef TEST +int main() { + int i = 0; + while(i != (int)_TZPLATFORM_VARIABLES_COUNT_) { + enum tzplatform_variable id = (enum tzplatform_variable)i; + const char*name = tzplatform_getname(id); + const char*value = tzplatform_getenv(id); + int xid = (int)tzplatform_getid(name); + printf("%d=%d\t%s=%s\n",i,xid,name,value); + i++; + } + return 0; +} +#endif diff --git a/src/tzplatform_config.h b/src/tzplatform_config.h new file mode 100644 index 0000000..496bb42 --- /dev/null +++ b/src/tzplatform_config.h @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ +#ifndef LIBTIZEN_PLATFORM_WRAPPER +#define LIBTIZEN_PLATFORM_WRAPPER + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* + Enforces the removal of the previously evaluated tizen platform variables. + + Call this function in case of changing of user inside the application. +*/ +extern +void tzplatform_reset(); + +/* + Return the count of variables. +*/ +extern +int tzplatform_getcount(); + +/* + Return the name of the variable 'id' as a string. + Return NULL if 'id' is invalid. +*/ +extern +const char* tzplatform_getname(enum tzplatform_variable id); + +/* + Return the id of the variable of 'name'. + Return _TZPLATFORM_VARIABLES_INVALID_ if 'name' doesn't match a + valid variable name. +*/ +extern +enum tzplatform_variable tzplatform_getid(const char *name); + +/* + Return the read-only string value of the tizen plaform variable 'id'. + The tizen global values are globals to the application. + Can return NULL in case of internal error or when 'id' isn't defined. +*/ +extern +const char* tzplatform_getenv(enum tzplatform_variable id); + +/* + Return the integer value of the tizen plaform variable 'id'. +*/ +extern +int tzplatform_getenv_int(enum tzplatform_variable id); + +/* + Return the string resulting of the concatenation of string value of the + tizen plaform variable 'id' and the given string 'str'. + + The returned value is a scratch buffer (unique for the thread) that is + available until the next call to 'tzplatform_mkstr', 'tzplatform_mkpath', or, + 'tzplatform_mkpath3'. + + Can return NULL in case of internal error. + + Example: + if TZ_SYS_HOME == "/opt/home" then calling + + tzplatform_mkstr(TZ_SYS_HOME,"-yes") + + will return "/opt/home-yes" +*/ +extern +const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str); + +/* + Return the string resulting of the path-concatenation of string value of the + tizen plaform variable 'id' and the given string 'path'. + + path-concatenation is the concatenation taking care of / characters. + + The returned value is a scratch buffer (unique for the thread) that is + available until the next call to 'tzplatform_mkstr', 'tzplatform_mkpath', or, + 'tzplatform_mkpath3'. + + Can return NULL in case of internal error. + + Example: + if TZ_SYS_HOME == "/opt/home" then calling + + tzplatform_mkpath(TZ_SYS_HOME,"yes") + + will return "/opt/home/yes" +*/ +extern +const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path); + +/* + Return the string resulting of the path-concatenation of string value of the + tizen plaform variable 'id' and the given strings 'path' and 'path2'. + + path-concatenation is the concatenation taking care of / characters. + + The returned value is a scratch buffer (unique for the thread) that is + available until the next call to 'tzplatform_mkstr', 'tzplatform_mkpath', or, + 'tzplatform_mkpath3'. + + Can return NULL in case of internal error. + + Example: + if TZ_SYS_HOME == "/opt/home" then calling + + tzplatform_mkpath3(TZ_SYS_HOME,"yes","no") + + will return "/opt/home/yes/no" +*/ +extern +const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, + const char* path2); + +/* + Return the uid for a given user name, stored in variable + Retun -1 in case of error. + + Example: + if TZ_USER_NAME=="app" then calling: + + tzplatform_getuid(TZ_USER_NAME) + + will return the uid of the user 'app' +*/ +extern +uid_t tzplatform_getuid(enum tzplatform_variable id); + +/* + Return the gid for a given group name, stored in variable + Retun -1 in case of error. + + Example: + if TZ_USER_GROUP=="app" then calling: + + tzplatform_getuid(TZ_USER_GROUP) + + will return the gid of the group 'app' +*/ +extern +gid_t tzplatform_getgid(enum tzplatform_variable id); + +#ifdef __cplusplus +} +#endif + +#endif // _LIBTIZEN-PLATFORM-WRAPPER diff --git a/src/tzplatform_get.c b/src/tzplatform_get.c new file mode 100644 index 0000000..a698b11 --- /dev/null +++ b/src/tzplatform_get.c @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2013 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * José Bollo + * Stéphane Desneux + * Jean-Benoit Martin + * + */ + +#include +#include +#include + +#include + +#define basename(x) (x) + +static const char usage [] = "type '%s --help' to get help.\n"; + +static const char help [] = "\ +\n\ +usage: %s [options...] [keys...]\n\ +\n\ +options:\n\ +\n\ +-a --all all keys (must not be followed by keys)\n\ +-n --not not in the keys\n\ +-e --export prefix with export\n\ +-l --list prints only the key names, not the values\n\ +-s --space separate with spaces instead of new-lines\n\ +-q --query silently check that given variables are existing\n\ +-c --continue continue to process if error\n\ +\n\ +"; + +int main(int argc, char **argv) +{ + char *progname = *argv++; + int all = 0, not = 0, query = 0, export = 0, space = 0, list = 0, cont = 0; + int i, n, *sel, p; + enum tzplatform_variable id; + + /* parse args */ + while(*argv && **argv=='-') { + char c, *opt = 1+*argv++; + while(opt) { + if (*opt == '-') { + char *x = 0; + c = *++opt; + switch(c) { + case 'a': x = "all"; break; + case 'n': x = "not"; break; + case 'q': x = "query"; break; + case 'e': x = "export"; break; + case 's': x = "space"; break; + case 'l': x = "list"; break; + case 'c': x = "continue"; break; + case 'h': x = "help"; break; + } + if (!x || strcmp(x,opt)) + c = 0; + opt = 0; + } + else { + c = *opt; + if (!*++opt) + opt = 0; + } + switch(c) { + case 'a': all = 1; break; + case 'n': not = 1; break; + case 'q': query = 1; break; + case 'e': export = 1; break; + case 's': space = 1; break; + case 'l': list = 1; break; + case 'c': cont = 1; break; + case 'h': + fprintf( stdout, help, basename(progname)); + return 0; + default: + fprintf( stderr, usage, basename(progname)); + return 1; + } + } + } + + /* some checks */ + if (query) { + if (all) { + fprintf( stderr, + "error! --all and --query aren't compatibles.\n"); + return 1; + } + if (not) { + fprintf( stderr, + "error! --not and --query aren't compatibles.\n"); + return 1; + } + if (list) { + fprintf( stderr, + "error! --list and --query aren't compatibles.\n"); + return 1; + } + if (cont) { + fprintf( stderr, + "error! --continue and --query aren't compatibles.\n"); + return 1; + } + if (space) { + fprintf( stderr, + "warning! --space option ignored for queries.\n"); + } + if (export) { + fprintf( stderr, + "warning! --export option ignored for queries.\n"); + } + } + if (all) { + if (*argv) { + fprintf( stderr, + "error! --all doesn't accept any key.\n"); + return 1; + } + /* all is like not nothing!! */ + not = 1; + } + + /* process */ + n = tzplatform_getcount(); + sel = calloc( sizeof(int), n); + if (sel == NULL) { + fprintf( stderr, "error! out of memory!\n"); + return 1; + } + + /* get the variables from the list */ + while (*argv) { + id = tzplatform_getid( *argv); + if (id == _TZPLATFORM_VARIABLES_INVALID_) { + if (query) + return 1; + fprintf( stderr, "error! %s isn't a variable.\n", *argv); + if (!cont) + return 1; + } + else { + sel[(int)id] = 1; + } + argv++; + } + + /* finished for queries */ + if (query) + return 0; + + /* emits the result */ + for (p = i = 0 ; i < n ; i++) { + if (sel[i] != not) { + if (p++) + printf( space ? " " : export ? "\nexport " : "\n"); + else if (export) + printf( "export "); + id = (enum tzplatform_variable) i; + printf( "%s", tzplatform_getname(id)); + if (!list) + printf( "=%s", tzplatform_getenv(id)); + } + } + if (p) + printf("\n"); + return 0; +} + -- 2.7.4