--- /dev/null
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 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 Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ 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 Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+ When we speak of free software, we are referring to freedom of use,
+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 and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+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 other code 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.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+^L
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard. To achieve this, non-free programs must be
+allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ 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, whereas the latter must
+be combined with the library in order to run.
+^L
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser 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.
+^L
+ 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.
+^L
+ 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.
+^L
+ 6. As an exception to the Sections above, you may also combine 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) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) 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.
+
+ d) 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.
+
+ e) 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 materials to be 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.
+^L
+ 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 with
+this License.
+^L
+ 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 Lesser 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.
+^L
+ 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
+^L
+ 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.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser 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.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
--- /dev/null
+- private functions and variables are prefixed with an underscore
+- all functions/types are prefixed with vsc_/Vsc or vsc_cli_/VscCli, the only
+ exceptions are static private functions and completely private types
+- use vsc_alloc/vsc_free instead of using calloc/free directly
+- use const pointers to indicate read-only usage of objects
+- indent with tabs, align with spaces, try to keep the line length below 80
+ characters assuming a tab is 4 spaces long
+- keep the usage of typedefs and macros at a minimal level to maintain good
+ code readability
+- read http://dl.fefe.de/optimizer-isec.pdf
+- use enums for constant values instead of defines, because enum values are
+ visible by name in gdb
+- write code in a way so that valgrind is happy
+- ...
--- /dev/null
+#!/usr/bin/make
+#
+# Makefile to build libvsccli and libvscmisc
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+SUBDIRS = cli misc include
+
+#
+# Rules
+#
+
+.PHONY: all clean install
+
+all: cli-all misc-all
+
+clean: cli-clean misc-clean
+
+install: cli-install misc-install include-install
+
+%-all:
+ @echo $@ | sed 's,\([a-z]*\)-all,echo "building \1"; $(MAKE) -C \1 all;,g' | sh
+
+%-clean:
+ @echo $@ | sed 's,\([a-z]*\)-clean,echo "cleaning \1"; $(MAKE) -C \1 clean;,g' | sh
+
+%-install:
+ @echo $@ | sed 's,\([a-z]*\)-install,echo "installing \1"; $(MAKE) -C \1 install;,g' | sh
+
+new:
+ $(MAKE) clean && $(MAKE)
--- /dev/null
+#!/usr/bin/make
+#
+# Makefile to build libvsccli
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+include ../config.mk
+
+OBJS = command.o core.o help.o option.o token.o
+TARGET = libvsccli.so
+
+#
+# Fancy gcc options
+#
+
+# libvscmisc
+#CFLAGS += -I ../include
+
+#
+# Rules
+#
+
+.PHONY: all clean install
+
+all: $(TARGET)
+
+clean:
+ rm -rf $(TARGET) $(OBJS)
+
+install: install-target-lib
+
+include ../rules.mk
--- /dev/null
+/*
+ * command.c: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <string.h>
+
+#include <libvscmisc/assert.h>
+#include <libvscmisc/error.h>
+#include <libvscmisc/list.h>
+#include <libvscmisc/memory.h>
+#include <libvscmisc/string.h>
+
+#include "../include/libvsccli/command.h"
+#include "../include/libvsccli/option.h"
+#include "command.h"
+#include "token.h"
+#include "option.h"
+
+extern const struct VscCliCommandInfo **_vsc_cli_command_infos;
+
+const struct VscCliCommandInfo *
+_vsc_cli_command_info_lookup (const char *command_name)
+{
+ int i;
+
+ for (i = 0; _vsc_cli_command_infos[i] != NULL; i++) {
+ if (strcmp (command_name, _vsc_cli_command_infos[i]->name) == 0) {
+ return _vsc_cli_command_infos[i];
+ }
+ }
+
+ return NULL;
+}
+
+static void
+_command_free (struct VscCliCommand **command)
+{
+ if (*command != NULL) {
+ vsc_cli_option_list_free (&(*command)->option_list);
+ }
+
+ vsc_free (command);
+}
+
+static struct VscCliCommand *
+_command_duplicate (struct VscError *error,
+ const struct VscCliCommand *command)
+{
+ struct VscCliCommand *command_duplicate = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ command_duplicate = vsc_memdup (error, command,
+ sizeof (struct VscCliCommand));
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ command_duplicate->option_list =
+ _vsc_cli_option_list_duplicate (error, command->option_list);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ command_duplicate->next = NULL;
+
+ return command_duplicate;
+
+failure:
+ _command_free (&command_duplicate);
+
+ return NULL;
+}
+
+void
+_vsc_cli_command_list_append (struct VscError *error,
+ struct VscCliCommand **command_list,
+ const struct VscCliCommand *command)
+{
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ vsc_list_append
+ (error, (struct VscList **) command_list,
+ (struct VscList *) command,
+ (VscList_DuplicateFunction) _command_duplicate);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return;
+ }
+}
+
+void
+vsc_cli_command_list_free (struct VscCliCommand **command_list)
+{
+ vsc_list_free ((struct VscList **) command_list,
+ (VscList_FreeFunction) _command_free);
+}
+
+void
+vsc_cli_command_parse (struct VscError *error,
+ const char *input, const char **input_tail,
+ struct VscCliCommand *command)
+{
+ int i;
+ char *command_name = NULL;
+ const struct VscCliCommandInfo *command_info;
+ const struct VscCliOptionInfo *option_info;
+ struct VscCliOption option;/*
+ char *token = NULL;*/
+ int num_options_parsed;
+ int data_option_index = 0;/*
+ int dash_count = 0;*/
+
+ *input_tail = input;
+
+ memset (command, 0, sizeof (struct VscCliCommand));
+
+ /*
+ * Parse command name.
+ */
+ do {
+ _vsc_cli_token_parse (error, *input_tail, &command_name, input_tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ } while (command_name == NULL && *input_tail != NULL);
+
+ if (command_name == NULL) {
+ if (*input_tail != NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expecting command in '%s'", input);
+ }
+
+ goto failure;
+ }
+
+ command_info = _vsc_cli_command_info_lookup (command_name);
+
+ if (command_info == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "command '%s' is unknown", command_name);
+
+ goto failure;
+ }
+
+ command->info = command_info;
+
+ /*
+ * Parse command options.
+ */
+ while (TRUE) {
+ num_options_parsed =
+ _vsc_cli_option_parse (error, command_info->option_infos,
+ data_option_index, command_name, *input_tail,
+ input_tail, &option);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (num_options_parsed > 0) {
+ if (option.info->type == VSC_CLI__OPTION_TYPE__DATA) {
+ ++data_option_index;
+ }
+
+ _vsc_cli_option_list_append (error, &command->option_list,
+ &option);
+ vsc_free (&option.string);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ } else {
+ break;
+ }
+
+
+
+ /*_vsc_cli_token_parse (error, *input_tail, &token, input_tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (token == NULL) {
+ break;
+ }
+
+ memset (&option, 0, sizeof (struct VscCliOption));
+
+ dash_count = 0;
+
+ if (*token == '-') {
+ ++dash_count;
+
+ if (*(token + 1) == '-') {
+ ++dash_count;
+ }
+ }
+
+ if (dash_count > 0) {
+ option_info = _vsc_cli_option_infos_lookup
+ (command_info->option_infos, token,
+ dash_count > 1);
+
+ if (option_info == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "unknown option %s for command '%s'",
+ token, command_name);
+ goto failure;
+ }
+
+ option.info = option_info;
+
+ switch (option_info->type) {
+ case VSC_CLI__OPTION_TYPE__BOOL:
+ break;
+
+ case VSC_CLI__OPTION_TYPE__NUMBER:
+ vsc_free (&token);
+ _vsc_cli_token_parse (error, *input_tail, &token, input_tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (token == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expected option syntax: %s",
+ _vsc_cli_option_syntax (option_info, FALSE));
+ goto failure;
+ }
+
+ option.number = vsc_strtoi (error, token, NULL, 10);
+
+ if (error->occured) {
+ vsc_error_cleanup (error);
+ vsc_error_init (error);
+
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expected option syntax: %s",
+ _vsc_cli_option_syntax (option_info, FALSE));
+ goto failure;
+ }
+
+ break;
+
+ case VSC_CLI__OPTION_TYPE__STRING:
+ vsc_free (&token);
+ _vsc_cli_token_parse (error, *input_tail, &token, input_tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (token == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expected option syntax: %s",
+ _vsc_cli_option_syntax (option_info, FALSE));
+ goto failure;
+ }
+
+ option.string = token;
+ break;
+
+ default:
+ VSC__ERROR1 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "internal error");
+ goto failure;
+ }
+
+ _vsc_cli_option_list_append (error, &command->option_list, &option);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ } else {
+ option_info = _vsc_cli_option_infos_lookup_data
+ (command_info->option_infos, data_count);
+
+ if (option_info == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "too many options for command '%s'",
+ command_name);
+
+ goto failure;
+ }
+
+ ++data_count;
+
+ option.info = option_info;
+ option.string = token;
+
+ _vsc_cli_option_list_append (error, &command->option_list, &option);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ }
+
+ vsc_free (&token);*/
+ }
+
+ /*
+ * Check required options.
+ */
+ for (i = 0; command_info->option_infos[i].long_name != NULL; ++i) {
+ option_info = &command_info->option_infos[i];
+
+ if (! option_info->required) {
+ continue;
+ }
+
+ if (_vsc_cli_option_list_lookup (command->option_list,
+ option_info->long_name) == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "command '%s' requires %s option",
+ command_info->name,
+ _vsc_cli_option_syntax (option_info, FALSE));
+
+ goto failure;
+ }
+ }
+
+cleanup:
+ vsc_free (&command_name);
+ //vsc_free (&token);
+
+ return;
+
+failure:
+ vsc_cli_option_list_free (&command->option_list);
+
+ goto cleanup;
+}
--- /dev/null
+/*
+ * command.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__COMMAND__PRIVATE_H__
+#define __VSC_CLI__COMMAND__PRIVATE_H__
+
+#include "../include/libvsccli/types.h"
+
+const struct VscCliCommandInfo *
+_vsc_cli_command_info_lookup (const char *command_name);
+
+void
+_vsc_cli_command_list_append (struct VscError *error,
+ struct VscCliCommand **command_list,
+ const struct VscCliCommand *command);
+
+#endif /* __VSC_CLI__COMMAND__PRIVATE_H__ */
--- /dev/null
+/*
+ * core.c: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <libvscmisc/assert.h>
+#include <libvscmisc/error.h>
+#include <libvscmisc/memory.h>
+
+#include "../include/libvsccli/command.h"
+#include "../include/libvsccli/core.h"
+#include "../include/libvsccli/help.h"
+#include "../include/libvsccli/option.h"
+#include "command.h"
+#include "option.h"
+
+static int _initialized = FALSE;
+
+const char *_vsc_cli_name = NULL;
+const char *_vsc_cli_help = NULL;
+const struct VscCliOptionInfo *_vsc_cli_option_infos = NULL;
+const struct VscCliCommandInfo **_vsc_cli_command_infos = NULL;
+
+void
+vsc_cli_init (struct VscError *error,
+ const char *name, const char *help,
+ const struct VscCliOptionInfo *option_infos,
+ const struct VscCliCommandInfo **command_infos)
+{
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ if (_initialized) {
+ VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_CALL,
+ "Already initialized!");
+ return;
+ }
+
+ if (_vsc_cli_option_infos_lookup_data (option_infos, 0) != 0) {
+ VSC__ERROR1 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
+ "Data options can not be used as direct options.");
+ return;
+ }
+
+ _vsc_cli_name = name;
+ _vsc_cli_help = help;
+ _vsc_cli_option_infos = option_infos;
+ _vsc_cli_command_infos = command_infos;
+
+ _initialized = TRUE;
+}
+
+void
+vsc_cli_cleanup (void)
+{
+ VSC__ASSERT (_initialized);
+
+ if (! _initialized) {
+ return;
+ }
+
+ _initialized = FALSE;
+}
+
+struct VscCliOption * /* option_list */
+vsc_cli_parse_option_list (struct VscError *error, const char *input,
+ const char **input_tail)
+{
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ int num_options_parsed;
+ struct VscCliOption option;
+ struct VscCliOption *option_list = NULL;
+
+ *input_tail = input;
+
+ while (*input_tail != NULL && **input_tail != '\0') {
+ num_options_parsed =
+ _vsc_cli_option_parse (error, _vsc_cli_option_infos, -1, NULL,
+ *input_tail, input_tail, &option);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (num_options_parsed > 0) {
+ _vsc_cli_option_list_append (error, &option_list, &option);
+ vsc_free (&option.string);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ } else {
+ break;
+ }
+ }
+
+ return option_list;
+
+failure:
+ vsc_cli_option_list_free (&option_list);
+
+ return NULL;
+}
+
+struct VscCliCommand * /* command_list */
+vsc_cli_parse_command_list (struct VscError *error, const char *input,
+ const char **input_tail)
+{
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ struct VscCliCommand command;
+ struct VscCliCommand *command_list = NULL;
+
+ *input_tail = input;
+
+ while (*input_tail != NULL && **input_tail != '\0') {
+ vsc_cli_command_parse (error, *input_tail, input_tail, &command);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ _vsc_cli_command_list_append (error, &command_list, &command);
+ vsc_cli_option_list_free (&command.option_list);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ }
+
+ return command_list;
+
+failure:
+ vsc_cli_command_list_free (&command_list);
+
+ return NULL;
+}
+
+void
+vsc_cli_run_command_list (struct VscError *error,
+ const struct VscCliCommand *command_list,
+ void *user_data)
+{
+ struct VscCliCommand *command;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ for (command = (struct VscCliCommand *) command_list; command != NULL;
+ command = command->next) {
+ command->info->function (error, command->option_list, user_data);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return;
+ }
+ }
+}
--- /dev/null
+/*
+ * help.c: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+
+#include <libvscmisc/assert.h>
+#include <libvscmisc/error.h>
+
+#include "../include/libvsccli/help.h"
+#include "../include/libvsccli/option.h"
+#include "command.h"
+#include "option.h"
+
+extern const char *_vsc_cli_name;
+extern const char *_vsc_cli_help;
+extern const struct VscCliOptionInfo *_vsc_cli_option_infos;
+extern const struct VscCliCommandInfo **_vsc_cli_command_infos;
+
+static void
+_help (struct VscError *error, const struct VscCliOption *option_list,
+ void *user_data);
+
+static const struct VscCliOptionInfo _help_option_infos[] = {
+ { -1, "command", "name of command", VSC_CLI__OPTION_TYPE__DATA, FALSE },
+ { -1, NULL, NULL, 0, FALSE },
+};
+
+const struct VscCliCommandInfo vsc_cli_help_command_info = {
+ "help",
+ "print help",
+ "Prints global or command specific help.",
+ _help,
+ _help_option_infos,
+};
+
+static void
+_help (struct VscError *error, const struct VscCliOption *option_list,
+ void *user_data VSC__ATTR__UNUSED)
+{
+ int i;
+ const char *command_name;
+ const struct VscCliCommandInfo *command_info;
+ const struct VscCliOptionInfo *option_info;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ command_name = vsc_cli_option_get_string (option_list, "command");
+
+ if (command_name != NULL) {
+ command_info = _vsc_cli_command_info_lookup (command_name);
+
+ if (command_info == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "help: command '%s' is unknown", command_name);
+ return;
+ }
+
+ printf ("\033[1mNAME\033[0m\n");
+ printf (" %s - %s\n", command_info->name, command_info->help);
+ printf ("\n");
+
+ printf ("\033[1mSYNOPSIS\033[0m\n");
+ printf (" %s", command_info->name);
+
+ for (i = 0; command_info->option_infos[i].long_name != NULL; ++i) {
+ option_info = &command_info->option_infos[i];
+
+ if (option_info->required) {
+ printf (" %s", _vsc_cli_option_syntax (option_info, TRUE));
+ } else {
+ printf (" [%s]", _vsc_cli_option_syntax (option_info, TRUE));
+ }
+ }
+
+ printf ("\n");
+ printf ("\n");
+
+ if (command_info->description != NULL &&
+ *command_info->description != '\0') {
+ printf ("\033[1mDESCRIPTION\033[0m\n");
+ printf (" %s\n", command_info->description);
+ printf ("\n");
+ }
+
+ if (command_info->option_infos[0].long_name != NULL) {
+ printf ("\033[1mOPTIONS\033[0m\n");
+
+ for (i = 0; command_info->option_infos[i].long_name != NULL; ++i) {
+ option_info = &command_info->option_infos[i];
+
+ printf (" %s\n"
+ " %s\n",
+ _vsc_cli_option_syntax (option_info, FALSE),
+ option_info->help);
+
+ if (command_info->option_infos[i + 1].long_name != NULL) {
+ printf ("\n");
+ }
+ }
+ }
+ } else {
+ printf ("\033[1mNAME\033[0m\n");
+ printf (" %s - %s\n", _vsc_cli_name, _vsc_cli_help);
+ printf ("\n");
+
+ printf ("\033[1mSYNOPSIS\033[0m\n");
+ printf (" %s", _vsc_cli_name);
+
+ for (i = 0; _vsc_cli_option_infos[i].long_name != NULL; ++i) {
+ option_info = &_vsc_cli_option_infos[i];
+
+ if (option_info->required) {
+ printf (" %s", _vsc_cli_option_syntax (option_info, TRUE));
+ } else {
+ printf (" [%s]", _vsc_cli_option_syntax (option_info, TRUE));
+ }
+ }
+
+ printf (" [<command>[, <command>]...]\n");
+
+ if (_vsc_cli_option_infos[0].long_name != NULL) {
+ printf ("\n");
+ printf ("\033[1mOPTIONS\033[0m\n");
+
+ for (i = 0; _vsc_cli_option_infos[i].long_name != NULL; ++i) {
+ option_info = &_vsc_cli_option_infos[i];
+
+ printf (" %s\n"
+ " %s\n",
+ _vsc_cli_option_syntax (option_info, FALSE),
+ option_info->help);
+
+ if (_vsc_cli_option_infos[i + 1].long_name != NULL) {
+ printf ("\n");
+ }
+ }
+ }
+
+ if (_vsc_cli_command_infos[0]->name != NULL) {
+ printf ("\n");
+ printf ("\033[1mCOMMANDS\033[0m\n");
+
+ for (i = 0; _vsc_cli_command_infos[i] != NULL; ++i) {
+ printf (" %-20s %s\n", _vsc_cli_command_infos[i]->name,
+ _vsc_cli_command_infos[i]->help);
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * option.c: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <string.h>
+
+#include <libvscmisc/assert.h>
+#include <libvscmisc/error.h>
+#include <libvscmisc/list.h>
+#include <libvscmisc/memory.h>
+#include <libvscmisc/string.h>
+
+#include "../include/libvsccli/option.h"
+#include "option.h"
+#include "token.h"
+
+const char *
+_vsc_cli_option_syntax (const struct VscCliOptionInfo *option_info,
+ int synopsis)
+{
+ static char buffer1[256];
+ static char buffer2[256];
+ static char buffer3[512];
+
+ switch (option_info->type) {
+ case VSC_CLI__OPTION_TYPE__DATA:
+ snprintf (buffer3, 512, "<%s>", option_info->long_name);
+ return buffer3;
+
+ case VSC_CLI__OPTION_TYPE__BOOL:
+ snprintf (buffer1, 256, "-%c", option_info->short_name);
+ snprintf (buffer2, 256, "--%s", option_info->long_name);
+ break;
+
+ case VSC_CLI__OPTION_TYPE__NUMBER:
+ snprintf (buffer1, 256, "-%c <number>", option_info->short_name);
+ snprintf (buffer2, 256, "--%s <number>", option_info->long_name);
+ break;
+
+ case VSC_CLI__OPTION_TYPE__STRING:
+ snprintf (buffer1, 256, "-%c <string>", option_info->short_name);
+ snprintf (buffer2, 256, "--%s <string>", option_info->long_name);
+ break;
+
+ default:
+ snprintf (buffer1, 256, "INVALID");
+ snprintf (buffer2, 256, "INVALID");
+ break;
+ }
+
+ if (synopsis && option_info->short_name != -1) {
+ return buffer1;
+ }
+
+ if (option_info->short_name == -1) {
+ return buffer2;
+ }
+
+ snprintf (buffer3, 512, "%s, %s", buffer1, buffer2);
+
+ return buffer3;
+}
+
+int /* num_options_parsed */
+_vsc_cli_option_parse (struct VscError *error,
+ const struct VscCliOptionInfo *option_infos,
+ int data_option_index, const char *command_name,
+ const char *input, const char **input_tail,
+ struct VscCliOption *option)
+{
+ int num_options_parsed = 1;
+ const struct VscCliOptionInfo *option_info;
+ const char *tail = NULL;
+ char *token = NULL;
+ int num_dashes = 0;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ *input_tail = input;
+
+ _vsc_cli_token_parse (error, input, &token, &tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (token == NULL) {
+ return 0;
+ }
+
+ memset (option, 0, sizeof (struct VscCliOption));
+
+ if (*token == '-') {
+ ++num_dashes;
+
+ if (*(token + 1) == '-') {
+ ++num_dashes;
+ }
+ }
+
+ if (num_dashes > 0) {
+ option_info = _vsc_cli_option_infos_lookup
+ (option_infos, token + num_dashes, num_dashes > 1);
+
+ if (option_info == NULL) {
+ if (command_name != NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "unknown option %s for command '%s'",
+ token, command_name);
+ } else {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "unknown option %s", token);
+ }
+
+ goto failure;
+ }
+
+ option->info = option_info;
+
+ switch (option_info->type) {
+ case VSC_CLI__OPTION_TYPE__BOOL:
+ break;
+
+ case VSC_CLI__OPTION_TYPE__NUMBER:
+ vsc_free (&token);
+ _vsc_cli_token_parse (error, tail, &token, &tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (token == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expected option syntax: %s",
+ _vsc_cli_option_syntax (option_info, FALSE));
+ goto failure;
+ }
+
+ option->number = vsc_strtoi (error, token, NULL, 10);
+
+ if (error->occured) {
+ vsc_error_cleanup (error);
+ vsc_error_init (error);
+
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expected option syntax: %s",
+ _vsc_cli_option_syntax (option_info, FALSE));
+ goto failure;
+ }
+
+ break;
+
+ case VSC_CLI__OPTION_TYPE__STRING:
+ vsc_free (&token);
+ _vsc_cli_token_parse (error, tail, &token, &tail);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (token == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "expected option syntax: %s",
+ _vsc_cli_option_syntax (option_info, FALSE));
+ goto failure;
+ }
+
+ option->string = token;
+ token = NULL;
+ break;
+
+ default:
+ VSC__ERROR1 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "internal error");
+ goto failure;
+ }
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ } else if (data_option_index >= 0) {
+ option_info = _vsc_cli_option_infos_lookup_data
+ (option_infos, data_option_index);
+
+ if (option_info == NULL) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "too many options for command '%s'",
+ command_name);
+ goto failure;
+ }
+
+ option->info = option_info;
+ option->string = token;
+ token = NULL;
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+ } else {
+ num_options_parsed = 0;
+ }
+
+ if (num_options_parsed > 0) {
+ *input_tail = tail;
+ }
+
+cleanup:
+ vsc_free (&token);
+
+ return num_options_parsed;
+
+failure:
+ vsc_free (&option->string);
+
+ num_options_parsed = -1;
+
+ goto cleanup;
+}
+
+static const struct VscCliOption *
+_option_get (const struct VscCliOption *option_list,
+ const char *option_long_name)
+{
+ const struct VscCliOption *option;
+
+ for (option = option_list; option != NULL; option = option->next) {
+ if (strcmp (option->info->long_name, option_long_name) == 0) {
+ return option;
+ }
+ }
+
+ return NULL;
+}
+
+int
+vsc_cli_option_get_bool (const struct VscCliOption *option_list,
+ const char *option_long_name)
+{
+ return _option_get (option_list, option_long_name) != NULL;
+}
+
+int
+vsc_cli_option_get_number (const struct VscCliOption *option_list,
+ const char *option_long_name, int *found)
+{
+ const struct VscCliOption *option;
+
+ if (found != NULL) {
+ *found = TRUE;
+ }
+
+ option = _option_get (option_list, option_long_name);
+
+ if (option == NULL) {
+ return 0;
+ }
+
+ if (found != NULL) {
+ *found = TRUE;
+ }
+
+ return option->number;
+}
+
+const char *
+vsc_cli_option_get_string (const struct VscCliOption *option_list,
+ const char *name)
+{
+ const struct VscCliOption *option;
+
+ option = _option_get (option_list, name);
+
+ if (option == NULL) {
+ return NULL;
+ }
+
+ return option->string;
+}
+
+const struct VscCliOptionInfo *
+_vsc_cli_option_infos_lookup (const struct VscCliOptionInfo *option_infos,
+ const char *option_name, int is_long)
+{
+ int i;
+
+ for (i = 0; option_infos[i].long_name != NULL; i++) {
+ if (option_infos[i].type == VSC_CLI__OPTION_TYPE__DATA) {
+ continue;
+ }
+
+ if (is_long) {
+ if (strcmp (option_name, option_infos[i].long_name) == 0) {
+ return &option_infos[i];
+ }
+ } else {
+ if (*option_name == option_infos[i].short_name) {
+ return &option_infos[i];
+ }
+ }
+ }
+
+ return NULL;
+}
+
+const struct VscCliOptionInfo *
+_vsc_cli_option_infos_lookup_data (const struct VscCliOptionInfo *option_infos,
+ int position)
+{
+ int i;
+
+ for (i = 0; option_infos[i].long_name != NULL; i++) {
+ if (option_infos[i].type == VSC_CLI__OPTION_TYPE__DATA) {
+ if (position > 0) {
+ --position;
+ } else {
+ return &option_infos[i];
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static void
+_option_free (struct VscCliOption **option)
+{
+ if (*option != NULL) {
+ vsc_free (&(*option)->string);
+ }
+
+ vsc_free (option);
+}
+
+static struct VscCliOption * /* option_duplicate */
+_option_duplicate (struct VscError *error, const struct VscCliOption *option)
+{
+ struct VscCliOption *option_duplicate = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ option_duplicate = vsc_memdup (error, option, sizeof (struct VscCliOption));
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ if (option->string != NULL) {
+ option_duplicate->string = vsc_strdup (error, option->string);
+ }
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ goto failure;
+ }
+
+ option_duplicate->next = NULL;
+
+ return option_duplicate;
+
+failure:
+ _option_free (&option_duplicate);
+
+ return NULL;
+}
+
+static int
+_option_match (struct VscList *item, void *match_data)
+{
+ struct VscCliOption *option = (struct VscCliOption *) item;
+ const char *option_long_name = (const char *) match_data;
+
+ return strcmp (option->info->long_name, option_long_name) == 0;
+}
+
+void
+_vsc_cli_option_list_append (struct VscError *error,
+ struct VscCliOption **option_list,
+ const struct VscCliOption *option)
+{
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ vsc_list_append
+ (error, (struct VscList **) option_list,
+ (struct VscList *) option,
+ (VscList_DuplicateFunction) _option_duplicate);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return;
+ }
+}
+
+void
+vsc_cli_option_list_free (struct VscCliOption **option_list)
+{
+ vsc_list_free ((struct VscList **) option_list,
+ (VscList_FreeFunction) _option_free);
+}
+
+struct VscCliOption * /* option_list_duplicate */
+_vsc_cli_option_list_duplicate (struct VscError *error,
+ const struct VscCliOption *option_list)
+{
+ struct VscCliOption *option_list_duplicate = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ option_list_duplicate = (struct VscCliOption *)
+ vsc_list_duplicate
+ (error, (struct VscList *) option_list,
+ (VscList_DuplicateFunction) _option_duplicate,
+ (VscList_FreeFunction) _option_free);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return NULL;
+ }
+
+ return option_list_duplicate;
+}
+
+struct VscCliOption *
+_vsc_cli_option_list_lookup (const struct VscCliOption *option_list,
+ const char *option_long_name)
+{
+ return (struct VscCliOption *)
+ vsc_list_lookup
+ ((struct VscList *) option_list,
+ (VscList_MatchFunction) _option_match,
+ (void *) option_long_name);
+}
--- /dev/null
+/*
+ * option.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__OPTION__PRIVATE_H__
+#define __VSC_CLI__OPTION__PRIVATE_H__
+
+#include "../include/libvsccli/types.h"
+
+const char *
+_vsc_cli_option_syntax (const struct VscCliOptionInfo *option_info,
+ int synopsis);
+
+int /* num_options_parsed */
+_vsc_cli_option_parse (struct VscError *error,
+ const struct VscCliOptionInfo *option_infos,
+ int data_option_index, const char *command_name,
+ const char *input, const char **input_tail,
+ struct VscCliOption *option);
+
+const struct VscCliOptionInfo *
+_vsc_cli_option_infos_lookup (const struct VscCliOptionInfo *option_infos,
+ const char *option_name, int is_long);
+
+const struct VscCliOptionInfo *
+_vsc_cli_option_infos_lookup_data (const struct VscCliOptionInfo *option_infos,
+ int position);
+
+struct VscCliOption * /* option_list_duplicate */
+_vsc_cli_option_list_duplicate (struct VscError *error,
+ const struct VscCliOption *option_list);
+
+void
+_vsc_cli_option_list_append (struct VscError *error,
+ struct VscCliOption **option_list,
+ const struct VscCliOption *option);
+
+struct VscCliOption *
+_vsc_cli_option_list_lookup (const struct VscCliOption *option_list,
+ const char *option_long_name);
+
+#endif /* __VSC_CLI__OPTION__PRIVATE_H__ */
--- /dev/null
+/*
+ * token.c: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <libvscmisc/assert.h>
+#include <libvscmisc/error.h>
+#include <libvscmisc/memory.h>
+
+#include "token.h"
+
+void
+_vsc_cli_token_parse (struct VscError *error, const char *input, char **token,
+ const char **input_tail)
+{
+ const char *ptr = input;
+ int quote = FALSE;
+ const char *start = NULL;
+ int length = 0;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ *token = NULL;
+
+ while (ptr != NULL && *ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) {
+ ++ptr;
+ }
+
+ if (ptr == NULL || *ptr == '\0') {
+ *input_tail = NULL;
+ return;
+ }
+
+ if (*ptr == ',') {
+ *input_tail = ++ptr;
+ return;
+ }
+
+ while (*ptr != '\0') {
+ if (quote == FALSE && (*ptr == ' ' || *ptr == '\t' || *ptr == ',')) {
+ break;
+ }
+
+ if (*ptr == '"') {
+ ++ptr;
+
+ if (! quote) {
+ quote = TRUE;
+ } else {
+ quote = FALSE;
+ break;
+ }
+ }
+
+ if (start == NULL) {
+ start = ptr;
+ }
+
+ ++ptr;
+ ++length;
+ }
+
+ if (quote) {
+ VSC__ERROR1 (error, VSC__ERROR_CODE__INTERNAL_ERROR,
+ "input is malformed, missing \"");
+ return;
+ }
+
+ *input_tail = ptr;
+
+ if (start == NULL || *start == '\0' || length == 0) {
+ return;
+ }
+
+ *token = vsc_alloc (error, length + 1);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return;
+ }
+
+ memcpy (*token, start, length);
+ (*token)[length] = '\0';
+}
--- /dev/null
+/*
+ * token.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__TOKEN_H__
+#define __VSC_CLI__TOKEN_H__
+
+#include "../include/libvsccli/types.h"
+
+void
+_vsc_cli_token_parse (struct VscError *error, const char *input, char **token,
+ const char **input_tail);
+
+#endif /* __VSC_CLI__TOKEN_H__ */
--- /dev/null
+#
+# General config for libvsccli and libvscmisc
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+CC = gcc
+PREFIX = /usr/local
+
+#
+# Fancy gcc options
+#
+
+CFLAGS += -fPIC
+
+# Daumenschrauben
+CFLAGS += -std=gnu99 -Wall -Werror -W -Wextra -Wmissing-declarations \
+ -Wmissing-prototypes -Wcast-align -Wpointer-arith -Wreturn-type \
+ -Wformat -Wformat-security -Wnested-externs -Wsign-compare \
+ -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Winline
+
+# Debugging
+CFLAGS += -O0 -g -ggdb
--- /dev/null
+#!/usr/bin/make
+#
+# Makefile to install the header files of libvscstar
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+#
+# Rules
+#
+
+.PHONY: install
+
+install: libvsccli-install libvscmisc-install
+
+%-install:
+ @echo $@ | sed 's,\([a-z]*\)-install,echo "installing include/\1"; $(MAKE) -C \1 install;,g' | sh
--- /dev/null
+#!/usr/bin/make
+#
+# Makefile to install the header files of libvsccli
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+include ../../config.mk
+
+HEADERS = command.h core.h help.h libvsccli.h option.h types.h
+TARGET = libvsccli
+
+#
+# Rules
+#
+
+.PHONY: install force
+
+install: $(HEADERS)
+
+include ../../rules.mk
+
--- /dev/null
+/*
+ * command.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__COMMAND_H__
+#define __VSC_CLI__COMMAND_H__
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void
+vsc_cli_command_list_free (struct VscCliCommand **command_list);
+
+void
+vsc_cli_command_parse (struct VscError *error, const char *input,
+ const char **input_tail, struct VscCliCommand *command);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_CLI__COMMAND_H__ */
--- /dev/null
+/*
+ * core.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__CORE_H__
+#define __VSC_CLI__CORE_H__
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void
+vsc_cli_init (struct VscError *error,
+ const char *name, const char *help,
+ const struct VscCliOptionInfo *option_infos,
+ const struct VscCliCommandInfo **command_infos);
+
+void
+vsc_cli_cleanup (void);
+
+struct VscCliOption * /* option_list */
+vsc_cli_parse_option_list (struct VscError *error, const char *input,
+ const char **input_tail);
+
+struct VscCliCommand * /* command_list */
+vsc_cli_parse_command_list (struct VscError *error, const char *input,
+ const char **input_tail);
+
+void
+vsc_cli_run_command_list (struct VscError *error,
+ const struct VscCliCommand *command_list,
+ void *user_data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_CLI__CORE_H__ */
--- /dev/null
+/*
+ * help.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__HELP_H__
+#define __VSC_CLI__HELP_H__
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern const struct VscCliCommandInfo vsc_cli_help_command_info;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_CLI__HELP_H__ */
--- /dev/null
+/*
+ * libvsccli.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI_H__
+#define __VSC_CLI_H__
+
+#include "command.h"
+#include "core.h"
+#include "help.h"
+#include "option.h"
+
+#endif /* __VSC_CLI_H__ */
--- /dev/null
+/*
+ * option.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__OPTION_H__
+#define __VSC_CLI__OPTION_H__
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int
+vsc_cli_option_get_bool (const struct VscCliOption *option_list,
+ const char *option_long_name);
+
+int
+vsc_cli_option_get_number (const struct VscCliOption *option_list,
+ const char *option_long_name, int *found);
+
+const char *
+vsc_cli_option_get_string (const struct VscCliOption *option_list,
+ const char *option_long_name);
+
+void
+vsc_cli_option_list_free (struct VscCliOption **option_list);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_CLI__OPTION_H__ */
--- /dev/null
+/*
+ * types.h: Library for interactive commandline interfaces
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_CLI__TYPES_H__
+#define __VSC_CLI__TYPES_H__
+
+#include <libvscmisc/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum VscCliOptionType {
+ VSC_CLI__OPTION_TYPE__DATA = 0,
+ VSC_CLI__OPTION_TYPE__BOOL,
+ VSC_CLI__OPTION_TYPE__NUMBER,
+ VSC_CLI__OPTION_TYPE__STRING,
+};
+
+struct VscCliOptionInfo {
+ const char short_name;
+ const char* long_name;
+ const char* help;
+ enum VscCliOptionType type;
+ int required;
+};
+
+struct VscCliOption {
+ struct VscCliOption *next;
+ const struct VscCliOptionInfo *info;
+ char* string;
+ int number;
+};
+
+typedef void (*VscCliCommandFunction) (struct VscError *error,
+ const struct VscCliOption *option_list,
+ void *user_data);
+
+struct VscCliCommandInfo {
+ const char* name;
+ const char* help;
+ const char* description;
+ VscCliCommandFunction function;
+ const struct VscCliOptionInfo *option_infos;
+};
+
+struct VscCliCommand {
+ struct VscCliCommand *next;
+ const struct VscCliCommandInfo *info;
+ struct VscCliOption *option_list;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_CLI__TYPES_H__ */
--- /dev/null
+#!/usr/bin/make
+#
+# Makefile to install the header files of libvscmisc
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+include ../../config.mk
+
+HEADERS = assert.h error.h libvscmisc.h list.h memory.h string.h types.h
+TARGET = libvscmisc
+
+#
+# Rules
+#
+
+.PHONY: install force
+
+install: $(HEADERS)
+
+include ../../rules.mk
+
--- /dev/null
+/*
+ * assert.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC__ASSERT_H__
+#define __VSC_MISC__ASSERT_H__
+
+#ifndef NDEBUG
+#define VSC__ASSERT(condition) \
+ do { \
+ if (! (condition)) { \
+ vsc_assert_failed (__FILE__, __LINE__, __FUNCTION__, #condition); \
+ } \
+ } while (0)
+#else
+#define VSC__ASSERT(condition) do { } while (0)
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void
+vsc_assert_failed (const char *file, int line, const char *function,
+ const char *condition);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_MISC__ASSERT_H__ */
--- /dev/null
+/*
+ * error.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC__ERROR_H__
+#define __VSC_MISC__ERROR_H__
+
+#include <stdio.h>
+
+#include "types.h"
+
+#define VSC__ERROR0(error, code) \
+ vsc_error_report (error, code, __FILE__, __LINE__, __FUNCTION__, NULL)
+
+#define VSC__ERROR1(error, code, message) \
+ vsc_error_report (error, code, __FILE__, __LINE__, __FUNCTION__, message)
+
+#define VSC__ERROR2(error, code, message, ...) \
+ vsc_error_report (error, code, __FILE__, __LINE__, __FUNCTION__, message, __VA_ARGS__)
+
+#define VSC__APPEND_ERROR0(error, code) \
+ vsc_error_append_report (error, code, __FILE__, __LINE__, __FUNCTION__, NULL)
+
+#define VSC__APPEND_ERROR1(error, code, message) \
+ vsc_error_append_report (error, code, __FILE__, __LINE__, __FUNCTION__, message)
+
+#define VSC__APPEND_ERROR2(error, code, message, ...) \
+ vsc_error_append_report (error, code, __FILE__, __LINE__, __FUNCTION__, message, __VA_ARGS__)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void
+vsc_error_init (struct VscError *error);
+
+void
+vsc_error_cleanup (struct VscError *error);
+
+/**
+ * Returns the string representaion of the error code.
+ *
+ * The string must not be freed by the caller.
+ */
+const char * /* error_string */
+vsc_error_string (enum VscErrorCode code);
+
+void
+vsc_error_fprint (struct VscError *error, FILE *stream);
+
+void
+vsc_error_report (struct VscError *error, enum VscErrorCode code,
+ const char *file, int line, const char *function,
+ const char *message, ...) VSC__ATTR__PRINTF (6, 7);
+
+void
+vsc_error_append_report (struct VscError *error, enum VscErrorCode code,
+ const char *file, int line, const char *function,
+ const char *message, ...) VSC__ATTR__PRINTF (6, 7);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_MISC__ERROR_H__ */
--- /dev/null
+/*
+ * libvscmisc.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC_H__
+#define __VSC_MISC_H__
+
+#include "assert.h"
+#include "error.h"
+#include "list.h"
+#include "memory.h"
+#include "string.h"
+
+#endif /* __VSC_MISC_H__ */
--- /dev/null
+/*
+ * list.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC__LIST_H__
+#define __VSC_MISC__LIST_H__
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Returns the last item from the list.
+ */
+struct VscList * /* last_item */
+vsc_list_get_last (struct VscList *list);
+
+/**
+ * Appends a duplicate of the item to the list.
+ *
+ * If an error occurs then the list is untouched.
+ */
+void
+vsc_list_append (struct VscError *error, struct VscList **list,
+ struct VscList *item,
+ VscList_DuplicateFunction duplicate_function);
+
+struct VscList * /* list_duplicate */
+vsc_list_duplicate (struct VscError *error, const struct VscList *list,
+ VscList_DuplicateFunction duplicate_function,
+ VscList_FreeFunction free_function);
+
+/**
+ * Looks up an item in the list.
+ *
+ * The first item for that the match function returns TRUE is returned. If no
+ * match occurs NULL is returned.
+ */
+struct VscList * /* item */
+vsc_list_lookup (const struct VscList *list,
+ VscList_MatchFunction match_function, void *match_data);
+
+/**
+ * Removes the item from the list and frees it.
+ *
+ * If the list becomes empty then the pointer to the list is set to NULL.
+ *
+ * If the item is not in the list then the list is untouched.
+ */
+void
+vsc_list_remove (struct VscList **list, struct VscList *item,
+ VscList_FreeFunction free_function);
+
+/**
+ * Recursively frees the list and the pointer to the list is set to NULL.
+ */
+void
+vsc_list_free (struct VscList **list, VscList_FreeFunction free_function);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_MISC__LIST_H__ */
--- /dev/null
+/*
+ * memory.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC__MEMORY_H__
+#define __VSC_MISC__MEMORY_H__
+
+#include <memory.h> /* for size_t */
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void *
+vsc_alloc (struct VscError *error, size_t size);
+
+/**
+ * Frees the block of memory at which the pointer pointed at by ptrptr points
+ * and sets the pointer pointed at by ptrptr to NULL.
+ *
+ * void *ptr = vsc_alloc (error, 42);
+ * ...
+ * vsc_free (&ptr);
+ */
+void
+vsc_free (void *ptrptr);
+
+void *
+vsc_memdup (struct VscError *error, const void *ptr, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_MISC__MEMORY_H__ */
--- /dev/null
+/*
+ * string.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC__STRING_H__
+#define __VSC_MISC__STRING_H__
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *
+vsc_strdup (struct VscError *error, const char *string);
+
+const char *
+vsc_strerror (int errnum);
+
+/**
+ * Stricter version of strtol, returning an int as result.
+ *
+ * If tail is NULL there must be no non-number character at the end of the
+ * string, otherwise an error occurs.
+ */
+int
+vsc_strtoi (struct VscError *error, const char *string, char **tail, int base);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_MISC__STRING_H__ */
--- /dev/null
+/*
+ * types.h: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __VSC_MISC__TYPES_H__
+#define __VSC_MISC__TYPES_H__
+
+#include <sys/time.h> /* for struct timeval */
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#define VSC__ATTR__UNUSED __attribute__((__unused__))
+#define VSC__ATTR__PRINTF(a, b) __attribute__ ((format (printf, a, b)))
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum VscErrorCode {
+ VSC__ERROR_CODE__UNDEFINED = 0,
+ VSC__ERROR_CODE__INTERNAL_ERROR,
+ VSC__ERROR_CODE__OUT_OF_MEMORY,
+ VSC__ERROR_CODE__XMLRPC_ERROR,
+ VSC__ERROR_CODE__PTHREAD_ERROR,
+ VSC__ERROR_CODE__LIBVIRT_ERROR,
+ VSC__ERROR_CODE__INVALID_ARGUMENT,
+ VSC__ERROR_CODE__INVALID_CALL,
+ VSC__ERROR_CODE__INVALID_UUID_STRING,
+ VSC__ERROR_CODE__INVALID_IPV4_STRING,
+ VSC__ERROR_CODE__INVALID_HOST_TYPE_STRING,
+ VSC__ERROR_CODE__NOT_INITIALIZED,
+ VSC__ERROR_CODE__NOT_IMPLEMENTED_YET,
+ VSC__ERROR_CODE__ALREADY_KNOWN,
+ VSC__ERROR_CODE__ERRNO,
+ VSC__ERROR_CODE__TRACE,
+};
+
+struct VscError {
+ struct VscError *next;
+ int occured;
+ enum VscErrorCode code;
+ char *message;
+ const char *file;
+ int line;
+ const char *function;
+ struct timeval timestamp;
+};
+
+struct VscList {
+ struct VscList *next;
+};
+
+typedef struct VscList * (*VscList_DuplicateFunction) (struct VscError *error,
+ struct VscList *item);
+typedef int (*VscList_MatchFunction) (struct VscList *item, void *match_data);
+typedef int (*VscList_FreeFunction) (struct VscList **item);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __VSC_MISC__TYPES_H__ */
--- /dev/null
+#!/usr/bin/make
+#
+# Makefile to build libvscmisc
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+include ../config.mk
+
+OBJS = assert.o error.o list.o memory.o string.o
+TARGET = libvscmisc.so
+
+#
+# Fancy gcc options
+#
+
+#
+# Rules
+#
+
+.PHONY: all clean install
+
+all: $(TARGET)
+
+clean:
+ rm -rf $(TARGET) $(OBJS)
+
+install: install-target-lib
+
+include ../rules.mk
--- /dev/null
+/*
+ * assert.c: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "../include/libvscmisc/assert.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+
+void
+vsc_assert_failed (const char *file, int line, const char *function,
+ const char *condition)
+{
+ struct timeval timestamp;
+ struct tm time_info;
+
+ gettimeofday (×tamp, NULL);
+ localtime_r (×tamp.tv_sec, &time_info);
+
+ fprintf (stderr, "%02d:%02d:%02d.%03d %s:%d %s : assertion '%s' failed\n",
+ time_info.tm_hour, time_info.tm_min, time_info.tm_sec,
+ (int) timestamp.tv_usec / 1000, file, line, function, condition);
+
+ abort();
+}
--- /dev/null
+/*
+ * error.c: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "../include/libvscmisc/assert.h"
+#include "../include/libvscmisc/error.h"
+#include "../include/libvscmisc/list.h"
+#include "../include/libvscmisc/memory.h"
+
+#define _ERROR__MESSAGE_SIZE 4096
+
+static const char *_error_default_message =
+ "Could not allocate memory for error message";
+
+static struct VscError _error_default = {
+ .next = NULL,
+ .occured = TRUE,
+ .code = VSC__ERROR_CODE__OUT_OF_MEMORY,
+ .message = NULL,
+ .file = "none",
+ .line = -1,
+ .function = "none",
+};
+
+static void
+_error_report_v (struct VscError *error, enum VscErrorCode code,
+ const char *file, int line, const char *function,
+ const char *message, va_list args)
+{
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+ VSC__ASSERT (error->message == NULL);
+ VSC__ASSERT (code != VSC__ERROR_CODE__UNDEFINED);
+
+ error->occured = TRUE;
+ error->code = code;
+ error->file = file;
+ error->line = line;
+ error->function = function;
+
+ if (message == NULL) {
+ error->message = NULL;
+ } else {
+ error->message = calloc (1, _ERROR__MESSAGE_SIZE);
+
+ if (error->message == NULL) {
+ error->message = (char *) _error_default_message;
+ } else {
+ vsnprintf (error->message, _ERROR__MESSAGE_SIZE, message, args);
+ }
+ }
+
+ gettimeofday (&error->timestamp, NULL);
+}
+
+const char *
+vsc_error_string (enum VscErrorCode code)
+{
+ switch (code) {
+ case VSC__ERROR_CODE__INTERNAL_ERROR:
+ return "Internal error";
+
+ case VSC__ERROR_CODE__OUT_OF_MEMORY:
+ return "Out of memory";
+
+ case VSC__ERROR_CODE__XMLRPC_ERROR:
+ return "XML-RPC error";
+
+ case VSC__ERROR_CODE__PTHREAD_ERROR:
+ return "Pthread error";
+
+ case VSC__ERROR_CODE__LIBVIRT_ERROR:
+ return "Libvirt error";
+
+ case VSC__ERROR_CODE__INVALID_ARGUMENT:
+ return "Invalid argument";
+
+ case VSC__ERROR_CODE__INVALID_CALL:
+ return "Invalid call";
+
+ case VSC__ERROR_CODE__INVALID_UUID_STRING:
+ return "Invalid UUID string";
+
+ case VSC__ERROR_CODE__INVALID_IPV4_STRING:
+ return "Invalid IPv4 string";
+
+ case VSC__ERROR_CODE__INVALID_HOST_TYPE_STRING:
+ return "Invalid host type string";
+
+ case VSC__ERROR_CODE__NOT_INITIALIZED:
+ return "Not initialized";
+
+ case VSC__ERROR_CODE__NOT_IMPLEMENTED_YET:
+ return "Not implemented yet";
+
+ case VSC__ERROR_CODE__ALREADY_KNOWN:
+ return "Already known";
+
+ case VSC__ERROR_CODE__ERRNO:
+ return "errno";
+
+ case VSC__ERROR_CODE__TRACE:
+ return "Inherited error";
+
+ default:
+ return "Undefined";
+ }
+}
+
+void
+vsc_error_fprint (struct VscError *error, FILE *stream)
+{
+ struct tm time_info;
+ struct VscError *error_next = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (error->occured);
+ VSC__ASSERT (stream != NULL);
+
+ if (! error->occured) {
+ return;
+ }
+
+ localtime_r (&error->timestamp.tv_sec, &time_info);
+
+ fprintf (stream, "%02d:%02d:%02d.%03d %s:%d %s : error [%s]",
+ time_info.tm_hour, time_info.tm_min, time_info.tm_sec,
+ (int) error->timestamp.tv_usec / 1000, error->file,
+ error->line, error->function,
+ vsc_error_string (error->code));
+
+ if (error->message != NULL) {
+ fprintf (stream, " %s\n", error->message);
+ } else {
+ fprintf (stream, "\n");
+ }
+
+ for (error_next = error->next; error_next != NULL;
+ error_next = error_next->next) {
+ fprintf (stream, " %s:%d %s : error [%s]",
+ error_next->file, error_next->line, error_next->function,
+ vsc_error_string (error_next->code));
+
+ if (error_next->message != NULL) {
+ fprintf (stream, " %s\n", error_next->message);
+ } else {
+ fprintf (stream, "\n");
+ }
+ }
+}
+
+void
+vsc_error_init (struct VscError *error)
+{
+ VSC__ASSERT (error != NULL);
+
+ error->next = NULL;
+ error->occured = FALSE;
+ error->code = VSC__ERROR_CODE__UNDEFINED;
+ error->file = NULL;
+ error->line = -1;
+ error->function = NULL;
+ error->message = NULL;
+
+ memset (&error->timestamp, 0, sizeof (struct timeval));
+}
+
+void
+vsc_error_cleanup (struct VscError *error)
+{
+ struct VscError *current;
+ struct VscError *next;
+
+ VSC__ASSERT (error != NULL);
+
+ if (error->message != _error_default_message) {
+ vsc_free (&error->message);
+ } else {
+ error->message = NULL;
+ }
+
+ /*
+ * Free the error list messages if they are allocated (not the default
+ * error message) and free the error structs too, if they are allocated
+ * (not the default error). But do not free the first element of the list
+ * because we do not "own" its memory.
+ */
+ current = error->next;
+
+ while (current != NULL) {
+ next = current->next;
+
+ if (current->message != _error_default_message) {
+ vsc_free (¤t->message);
+ } else {
+ current->message = NULL;
+ }
+
+ if (current != &_error_default) {
+ vsc_free (¤t);
+ }
+
+ current = next;
+ }
+}
+
+void
+vsc_error_report (struct VscError *error, enum VscErrorCode code,
+ const char *file, int line, const char *function,
+ const char *message, ...)
+{
+ va_list args;
+
+ va_start (args, message);
+ _error_report_v (error, code, file, line, function, message, args);
+ va_end (args);
+}
+
+void
+vsc_error_append_report (struct VscError *error, enum VscErrorCode code,
+ const char *file, int line, const char *function,
+ const char *message, ...)
+{
+ va_list args;
+ struct VscError *error_last;
+ struct VscError *error_next;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (error->occured);
+ VSC__ASSERT (code != VSC__ERROR_CODE__UNDEFINED);
+
+ error_last = (struct VscError *)
+ vsc_list_get_last ((struct VscList *) error);
+
+ /*
+ * Do not append to the default error, because if it is appended we are
+ * already in an out-of-memory situation.
+ */
+ if (error_last == &_error_default) {
+ return;
+ }
+
+ error_next = calloc (1, sizeof (struct VscError));
+
+ if (error_next == NULL) {
+ error_next = &_error_default;
+ } else {
+ va_start (args, message);
+ _error_report_v (error_next, code, file, line, function, message,
+ args);
+ va_end (args);
+ }
+
+ error_last->next = error_next;
+}
--- /dev/null
+/*
+ * list.c: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdlib.h>
+
+#include "../include/libvscmisc/assert.h"
+#include "../include/libvscmisc/error.h"
+#include "../include/libvscmisc/list.h"
+
+/**
+ * Returns the last item from the list.
+ */
+struct VscList * /* last_item */
+vsc_list_get_last (struct VscList *list)
+{
+ struct VscList *item;
+
+ if (list == NULL) {
+ return NULL;
+ }
+
+ item = list;
+
+ while (item->next != NULL) {
+ item = item->next;
+ }
+
+ return item;
+}
+
+/**
+ * Appends a duplicate of the item to the list.
+ *
+ * If an error occurs then the list is untouched.
+ */
+void
+vsc_list_append (struct VscError *error, struct VscList **list,
+ struct VscList *item,
+ VscList_DuplicateFunction duplicate_function)
+{
+ struct VscList *duplicate = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ duplicate = duplicate_function (error, item);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return;
+ }
+
+ if (*list == NULL) {
+ *list = duplicate;
+ return;
+ }
+
+ vsc_list_get_last (*list)->next = duplicate;
+}
+
+struct VscList * /* list_duplicate */
+vsc_list_duplicate (struct VscError *error, const struct VscList *list,
+ VscList_DuplicateFunction duplicate_function,
+ VscList_FreeFunction free_function)
+{
+ struct VscList *item = NULL;
+ struct VscList *list_duplicate = NULL;
+
+ for (item = (struct VscList *) list; item != NULL; item = item->next) {
+ vsc_list_append (error, &list_duplicate, item, duplicate_function);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+
+ vsc_list_free (&list_duplicate, free_function);
+
+ return NULL;
+ }
+ }
+
+ return list_duplicate;
+}
+
+/**
+ * Looks up an item in the list.
+ *
+ * The first item for that the match function returns TRUE is returned. If no
+ * match occurs NULL is returned.
+ */
+struct VscList * /* item */
+vsc_list_lookup (const struct VscList *list,
+ VscList_MatchFunction match_function, void *match_data)
+{
+ struct VscList *item = NULL;
+
+ for (item = (struct VscList *) list; item != NULL; item = item->next) {
+ if (match_function (item, match_data)) {
+ return item;
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * Removes the item from the list and frees it.
+ *
+ * If the list becomes empty then the pointer to the list is set to NULL.
+ *
+ * If the item is not in the list then the list is untouched.
+ */
+void
+vsc_list_remove (struct VscList **list, struct VscList *item,
+ VscList_FreeFunction free_function)
+{
+ struct VscList *previous_item = NULL;
+ struct VscList *current_item = NULL;
+
+ for (current_item = *list; current_item != NULL;
+ current_item = current_item->next) {
+ if (current_item == item) {
+ if (previous_item != NULL) {
+ previous_item->next = current_item->next;
+ current_item->next = NULL;
+
+ free_function (¤t_item);
+
+ return;
+ } else {
+ *list = current_item->next;
+ current_item->next = NULL;
+
+ free_function (¤t_item);
+
+ return;
+ }
+ }
+
+ previous_item = current_item;
+ }
+}
+
+/**
+ * Recursively frees the list and set the list pointer to NULL.
+ */
+void
+vsc_list_free (struct VscList **list, VscList_FreeFunction free_function)
+{
+ struct VscList *item;
+ struct VscList *next_item;
+
+ if (*list == NULL) {
+ return;
+ }
+
+ item = *list;
+ *list = NULL;
+
+ do {
+ next_item = item->next;
+
+ free_function (&item);
+
+ item = next_item;
+ } while (item != NULL);
+}
--- /dev/null
+/*
+ * memory.c: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <malloc.h>
+#include <memory.h>
+
+#include "../include/libvscmisc/assert.h"
+#include "../include/libvscmisc/error.h"
+#include "../include/libvscmisc/memory.h"
+
+void *
+vsc_alloc (struct VscError *error, size_t size)
+{
+ void *ptr = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ ptr = calloc (1, size);
+
+ if (ptr == NULL) {
+ VSC__ERROR0 (error, VSC__ERROR_CODE__OUT_OF_MEMORY);
+ return NULL;
+ }
+
+ return ptr;
+}
+
+/**
+ * Frees the block of memory at which the pointer pointed at by ptrptr points
+ * and sets the pointer pointed at by ptrptr to NULL.
+ *
+ * void *ptr = vsc_alloc (error, 42);
+ * ...
+ * vsc_free (&ptr);
+ */
+void
+vsc_free (void *ptrptr)
+{
+ VSC__ASSERT (ptrptr != NULL);
+
+ free (*(void **) ptrptr);
+
+ *(void **) ptrptr = NULL;
+}
+
+void *
+vsc_memdup (struct VscError *error, const void *ptr, size_t size)
+{
+ void *duplicate = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ duplicate = vsc_alloc (error, size);
+
+ if (error->occured) {
+ VSC__APPEND_ERROR0 (error, VSC__ERROR_CODE__TRACE);
+ return NULL;
+ }
+
+ memcpy (duplicate, ptr, size);
+
+ return duplicate;
+}
--- /dev/null
+/*
+ * string.c: Library of miscellaneous stuff for other libvsc* libraries
+ *
+ * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../include/libvscmisc/assert.h"
+#include "../include/libvscmisc/error.h"
+#include "../include/libvscmisc/string.h"
+
+char *
+vsc_strdup (struct VscError *error, const char *string)
+{
+ char *string_duplicate = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ string_duplicate = strdup (string);
+
+ if (string_duplicate == NULL) {
+ VSC__ERROR0 (error, VSC__ERROR_CODE__OUT_OF_MEMORY);
+ }
+
+ return string_duplicate;
+}
+
+const char *
+vsc_strerror (int errnum)
+{
+ switch (errnum) {
+ case EAGAIN:
+ return "EAGAIN";
+
+ case EBUSY:
+ return "EBUSY";
+
+ case EINVAL:
+ return "EINVAL";
+
+ case ENOMEM:
+ return "ENOMEM";
+
+ case EPERM:
+ return "EPERM";
+
+ case ERANGE:
+ return "EINVAL";
+
+ default:
+ return "Undefined";
+ }
+}
+
+/**
+ * Stricter version of strtol, returning an int as result.
+ *
+ * If tail is NULL there must be no non-number character at the end of the
+ * string, otherwise an error occurs.
+ */
+int
+vsc_strtoi (struct VscError *error, const char *string, char **tail, int base)
+{
+ long int value = 0;
+ char *temp = NULL;
+
+ VSC__ASSERT (error != NULL);
+ VSC__ASSERT (! error->occured);
+
+ errno = 0;
+ value = strtol (string, &temp, base);
+
+ if (errno != 0) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__ERRNO,
+ "Converting '%s' yields errno %s (%d)", string,
+ vsc_strerror (errno), errno);
+ }
+ else if (tail == NULL && *temp != '\0') {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
+ "Expecting '\\0' after the last digit in '%s' "
+ "but found '%c'", string, *temp);
+ }
+ else if (temp == string) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
+ "Nothing of '%s' was converted", string);
+ }
+ else if ((int) value != value) {
+ VSC__ERROR2 (error, VSC__ERROR_CODE__INVALID_ARGUMENT,
+ "'%s' is not representable as int", string);
+ }
+
+ if (tail != NULL) {
+ *tail = temp;
+ }
+
+ return value;
+}
--- /dev/null
+#
+# General rules for libvsccli and libvscmisc
+#
+# Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
+#
+
+#
+# Rules
+#
+
+%.o: %.c
+ @echo "> compiling $<"
+ $(CC) $(CFLAGS) -c $<
+
+%.a: $(OBJS)
+ @echo "> linking $@"
+ ar rcs $@ $(OBJS)
+
+%.so: $(OBJS)
+ @echo "> linking $@"
+ $(CC) -shared -o $@ $(OBJS) $(LDFLAGS)
+
+install-target-lib:
+ mkdir -p $(PREFIX)/lib
+ @echo "> installing $(TARGET)"
+ install -c -m 755 $(TARGET) $(PREFIX)/lib/$(TARGET)
+
+force:
+
+%.h: force
+ mkdir -p $(PREFIX)/include/$(TARGET)
+ @echo "> installing $@"
+ install -c -m 644 $@ $(PREFIX)/include/$(TARGET)/$@