diff options
author | Bryan Jurish <mukau@users.sourceforge.net> | 2008-11-29 23:22:39 +0000 |
---|---|---|
committer | Bryan Jurish <mukau@users.sourceforge.net> | 2008-11-29 23:22:39 +0000 |
commit | d13da71edce4b913736c1e752a211ae20c193292 (patch) | |
tree | 57b0c3884b5d465259d21906f89e4ec5635da9d3 /gfsm/gfsm/src/programs | |
parent | 2072ea2ef54b92775efc83c82d9a4b7a8ac4d616 (diff) |
+ added local copy of gfsm source tree in gfsm/ subdir (for pd-extended auto-builds)
+ external builds now use static local libgfsm by default (insulates vs. API change, etc.)
svn path=/trunk/externals/moocow/; revision=10403
Diffstat (limited to 'gfsm/gfsm/src/programs')
198 files changed, 25624 insertions, 0 deletions
diff --git a/gfsm/gfsm/src/programs/.cvsignore b/gfsm/gfsm/src/programs/.cvsignore new file mode 100644 index 0000000..ccc326d --- /dev/null +++ b/gfsm/gfsm/src/programs/.cvsignore @@ -0,0 +1,79 @@ +gfsmindex +gfsmarith +gfsmheader +gfsmoptional +gfsmreplace +gfsmsigma +gfsmarcsort +gfsmclosure +gfsmcompile +gfsmcomplement +gfsmcompose +gfsmcompre +gfsmconcat +gfsmconnect +gfsmconvert +gfsmdeterminize +gfsmdifference +gfsmdraw +gfsminfo +gfsmintersect +gfsminvert +gfsmlabels +gfsmlookup +gfsmprint +gfsmproduct +gfsmproject +gfsmprune +gfsmrenumber +gfsmreverse +gfsmrmepsilon +gfsmstrings +gfsmunion +gfsmviterbi + +*.out +*.tmp +tmp.* +tmp +SFST* + +*~ +.*~ +*.o +*.lo +*.la +.libs +*.a +*.so +.deps +*.ps +*.lfsa +*.fst +#*.tfst +*.fsa +#*.tfsa +*.thfst +*.afst +*.afsa +*.gfsa +*.gfst +*.dot +#*.lab +*.scl + +#*_cmdparser.c +#*_cmdparser.h + +argh +save +tdata + +stamp-h* +aclocal.m4 +Makefile +Makefile.in +README.txt +configure + +ma-dwds-hacks diff --git a/gfsm/gfsm/src/programs/Makefile.am b/gfsm/gfsm/src/programs/Makefile.am new file mode 100644 index 0000000..b59e087 --- /dev/null +++ b/gfsm/gfsm/src/programs/Makefile.am @@ -0,0 +1,613 @@ +## File: src/programs/Makefile.am +## Package: gfsm +## Description: +## + src-level automake file +## +## Process this file with Automake to create Makefile.in. +##----------------------------------------------------------------------- + +##----------------------------------------------------------------------- +## subdirectories +##----------------------------------------------------------------------- +#SUBDIRS = + +##----------------------------------------------------------------------- +## primary targets +##----------------------------------------------------------------------- + +## --- executable programs +#bin_PROGRAMS = ${prog_1} ... ${prog_N} +bin_PROGRAMS = \ + gfsmarcsort \ + gfsmarith \ + gfsmclosure \ + gfsmcompile \ + gfsmcomplement \ + gfsmcompose \ + gfsmcompre \ + gfsmconcat \ + gfsmconnect \ + gfsmconvert \ + gfsmdeterminize \ + gfsmdifference \ + gfsmdraw \ + gfsmheader \ + gfsminfo \ + gfsmintersect \ + gfsminvert \ + gfsmlabels \ + gfsmlookup \ + gfsmoptional \ + gfsmprint \ + gfsmproduct \ + gfsmproject \ + gfsmrenumber \ + gfsmreplace \ + gfsmreverse \ + gfsmrmepsilon \ + gfsmsigma \ + gfsmstrings \ + gfsmunion \ + gfsmviterbi + +EXTRA_PROGRAMS = gfsmindex +bin_PROGRAMS += gfsmindex + +EXTRA_DIST = + +#bin_SCRIPTS = ${script1} ... ${scriptN} + +## --- libtool libraries +#lib_LTLIBRARIES = ${lib_1}.la ... ${lib_N}.la + +## --- shared data in @pkgdatadir@ +#pkgdata_DATA = ${data_1} ... ${data_N} + +## --- manpages -- section is auto-detected +#man_MANS = ${man_1} ... ${man_N} + +##----------------------------------------------------------------------- +## sources +##----------------------------------------------------------------------- + +SUFFIXES = \ + .gog _cmdparser.c _cmdparser.cc _cmdparser.h \ + .pod .txt .1 .html .tex + + +##----------------------------------------------------------------------- +## individual programs +##----------------------------------------------------------------------- + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +## --- Required sources +#${prog_i}_SOURCES = +## +## --- Extra (possible)sources +#EXTRA_${prog_i}_SOURCES = +#EXTRA_${lib_i}_la_SOURCES = +## +## --- leftovers +#mootconfig_SOURCES = mootconfig.cc +## +## --- Additional dependencies +#gfsmcompile_main.o: gfsmcompile_cmdparser.h +## +## --- Linker flags +#${prog_i}_LDFLAGS = -L. -static +#${prog_i}_LDADD = ${non_src_file}.o -lsomelib +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +#gfsmX_SOURCES = \ +# gfsmX_main.c \ +# gfsmX_cmdparser.c gfsmX_cmdparser.h +# +#gfsmX_main.o: gfsmX_cmdparser.h +# +#gfsmX_LDFLAGS = $(LDFLAGS_COMMON) +#gfsmX_LDADD = $(LDADD_COMMON) +# +#EXTRA_DIST += gfsmX.gog +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmarith_SOURCES = \ + gfsmarith_main.c \ + gfsmarith_cmdparser.c gfsmarith_cmdparser.h + +gfsmarith_main.o: gfsmarith_cmdparser.h + +gfsmarith_LDFLAGS = $(LDFLAGS_COMMON) +gfsmarith_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmarith.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmcompile_SOURCES = \ + gfsmcompile_main.c \ + gfsmcompile_cmdparser.c gfsmcompile_cmdparser.h + +gfsmcompile_main.o: gfsmcompile_cmdparser.h + +gfsmcompile_LDFLAGS = $(LDFLAGS_COMMON) +gfsmcompile_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmcompile.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmconvert_SOURCES = \ + gfsmconvert_main.c \ + gfsmconvert_cmdparser.c gfsmconvert_cmdparser.h + +gfsmconvert_main.o: gfsmconvert_cmdparser.h + +gfsmconvert_LDFLAGS = $(LDFLAGS_COMMON) +gfsmconvert_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmconvert.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmprint_SOURCES = \ + gfsmprint_main.c \ + gfsmprint_cmdparser.c gfsmprint_cmdparser.h + +gfsmprint_main.o: gfsmprint_cmdparser.h + +gfsmprint_LDFLAGS = $(LDFLAGS_COMMON) +gfsmprint_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmprint.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmindex_SOURCES = \ + gfsmindex_main.c \ + gfsmindex_cmdparser.c gfsmindex_cmdparser.h + +gfsmindex_main.o: gfsmindex_cmdparser.h + +gfsmindex_LDFLAGS = $(LDFLAGS_COMMON) +gfsmindex_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmindex.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsminfo_SOURCES = \ + gfsminfo_main.c \ + gfsminfo_cmdparser.c gfsminfo_cmdparser.h + +gfsminfo_main.o: gfsminfo_cmdparser.h + +gfsminfo_LDFLAGS = $(LDFLAGS_COMMON) +gfsminfo_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsminfo.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmarcsort_SOURCES = \ + gfsmarcsort_main.c \ + gfsmarcsort_cmdparser.c gfsmarcsort_cmdparser.h + +gfsmarcsort_main.o: gfsmarcsort_cmdparser.h + +gfsmarcsort_LDFLAGS = $(LDFLAGS_COMMON) +gfsmarcsort_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmarcsort.gog +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmcomplement_SOURCES = \ + gfsmcomplement_main.c \ + gfsmcomplement_cmdparser.c gfsmcomplement_cmdparser.h + +gfsmcomplement_main.o: gfsmcomplement_cmdparser.h + +gfsmcomplement_LDFLAGS = $(LDFLAGS_COMMON) +gfsmcomplement_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmcomplement.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmcompose_SOURCES = \ + gfsmcompose_main.c \ + gfsmcompose_cmdparser.c gfsmcompose_cmdparser.h + +gfsmcompose_main.o: gfsmcompose_cmdparser.h + +gfsmcompose_LDFLAGS = $(LDFLAGS_COMMON) +gfsmcompose_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmcompose.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmcompre_SOURCES = \ + gfsmcompre_main.c \ + gfsmcompre_cmdparser.c gfsmcompre_cmdparser.h + +gfsmcompre_main.o: gfsmcompre_cmdparser.h + +gfsmcompre_LDFLAGS = $(LDFLAGS_COMMON) +gfsmcompre_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmcompre.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmconcat_SOURCES = \ + gfsmconcat_main.c \ + gfsmconcat_cmdparser.c gfsmconcat_cmdparser.h + +gfsmconcat_main.o: gfsmconcat_cmdparser.h + +gfsmconcat_LDFLAGS = $(LDFLAGS_COMMON) +gfsmconcat_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmconcat.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmclosure_SOURCES = \ + gfsmclosure_main.c \ + gfsmclosure_cmdparser.c gfsmclosure_cmdparser.h + +gfsmclosure_main.o: gfsmclosure_cmdparser.h + +gfsmclosure_LDFLAGS = $(LDFLAGS_COMMON) +gfsmclosure_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmclosure.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmconnect_SOURCES = \ + gfsmconnect_main.c \ + gfsmconnect_cmdparser.c gfsmconnect_cmdparser.h + +gfsmconnect_main.o: gfsmconnect_cmdparser.h + +gfsmconnect_LDFLAGS = $(LDFLAGS_COMMON) +gfsmconnect_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmconnect.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmdeterminize_SOURCES = \ + gfsmdeterminize_main.c \ + gfsmdeterminize_cmdparser.c gfsmdeterminize_cmdparser.h + +gfsmdeterminize_main.o: gfsmdeterminize_cmdparser.h + +gfsmdeterminize_LDFLAGS = $(LDFLAGS_COMMON) +gfsmdeterminize_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmdeterminize.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmdifference_SOURCES = \ + gfsmdifference_main.c \ + gfsmdifference_cmdparser.c gfsmdifference_cmdparser.h + +gfsmdifference_main.o: gfsmdifference_cmdparser.h + +gfsmdifference_LDFLAGS = $(LDFLAGS_COMMON) +gfsmdifference_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmdifference.gog + + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmdraw_SOURCES = \ + gfsmdraw_main.c \ + gfsmdraw_cmdparser.c gfsmdraw_cmdparser.h + +gfsmdraw_main.o: gfsmdraw_cmdparser.h + +gfsmdraw_LDFLAGS = $(LDFLAGS_COMMON) +gfsmdraw_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmdraw.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmheader_SOURCES = \ + gfsmheader_main.c \ + gfsmheader_cmdparser.c gfsmheader_cmdparser.h + +gfsmheader_main.o: gfsmheader_cmdparser.h + +gfsmheader_LDFLAGS = $(LDFLAGS_COMMON) +gfsmheader_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmheader.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmintersect_SOURCES = \ + gfsmintersect_main.c \ + gfsmintersect_cmdparser.c gfsmintersect_cmdparser.h + +gfsmintersect_main.o: gfsmintersect_cmdparser.h + +gfsmintersect_LDFLAGS = $(LDFLAGS_COMMON) +gfsmintersect_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmintersect.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsminvert_SOURCES = \ + gfsminvert_main.c \ + gfsminvert_cmdparser.c gfsminvert_cmdparser.h + +gfsminvert_main.o: gfsminvert_cmdparser.h + +gfsminvert_LDFLAGS = $(LDFLAGS_COMMON) +gfsminvert_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsminvert.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmlabels_SOURCES = \ + gfsmlabels_main.c \ + gfsmlabels_cmdparser.c gfsmlabels_cmdparser.h + +gfsmlabels_main.o: gfsmlabels_cmdparser.h + +gfsmlabels_LDFLAGS = $(LDFLAGS_COMMON) +gfsmlabels_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmlabels.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmlookup_SOURCES = \ + gfsmlookup_main.c \ + gfsmlookup_cmdparser.c gfsmlookup_cmdparser.h + +gfsmlookup_main.o: gfsmlookup_cmdparser.h + +gfsmlookup_LDFLAGS = $(LDFLAGS_COMMON) +gfsmlookup_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmlookup.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmoptional_SOURCES = \ + gfsmoptional_main.c \ + gfsmoptional_cmdparser.c gfsmoptional_cmdparser.h + +gfsmoptional_main.o: gfsmoptional_cmdparser.h + +gfsmoptional_LDFLAGS = $(LDFLAGS_COMMON) +gfsmoptional_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmoptional.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmproduct_SOURCES = \ + gfsmproduct_main.c \ + gfsmproduct_cmdparser.c gfsmproduct_cmdparser.h + +gfsmproduct_main.o: gfsmproduct_cmdparser.h + +gfsmproduct_LDFLAGS = $(LDFLAGS_COMMON) +gfsmproduct_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmproduct.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmproject_SOURCES = \ + gfsmproject_main.c \ + gfsmproject_cmdparser.c gfsmproject_cmdparser.h + +gfsmproject_main.o: gfsmproject_cmdparser.h + +gfsmproject_LDFLAGS = $(LDFLAGS_COMMON) +gfsmproject_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmproject.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmrenumber_SOURCES = \ + gfsmrenumber_main.c \ + gfsmrenumber_cmdparser.c gfsmrenumber_cmdparser.h + +gfsmrenumber_main.o: gfsmrenumber_cmdparser.h + +gfsmrenumber_LDFLAGS = $(LDFLAGS_COMMON) +gfsmrenumber_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmrenumber.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmreplace_SOURCES = \ + gfsmreplace_main.c \ + gfsmreplace_cmdparser.c gfsmreplace_cmdparser.h + +gfsmreplace_main.o: gfsmreplace_cmdparser.h + +gfsmreplace_LDFLAGS = $(LDFLAGS_COMMON) +gfsmreplace_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmreplace.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmreverse_SOURCES = \ + gfsmreverse_main.c \ + gfsmreverse_cmdparser.c gfsmreverse_cmdparser.h + +gfsmreverse_main.o: gfsmreverse_cmdparser.h + +gfsmreverse_LDFLAGS = $(LDFLAGS_COMMON) +gfsmreverse_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmreverse.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmrmepsilon_SOURCES = \ + gfsmrmepsilon_main.c \ + gfsmrmepsilon_cmdparser.c gfsmrmepsilon_cmdparser.h + +gfsmrmepsilon_main.o: gfsmrmepsilon_cmdparser.h + +gfsmrmepsilon_LDFLAGS = $(LDFLAGS_COMMON) +gfsmrmepsilon_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmrmepsilon.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmsigma_SOURCES = \ + gfsmsigma_main.c \ + gfsmsigma_cmdparser.c gfsmsigma_cmdparser.h + +gfsmsigma_main.o: gfsmsigma_cmdparser.h + +gfsmsigma_LDFLAGS = $(LDFLAGS_COMMON) +gfsmsigma_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmsigma.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmstrings_SOURCES = \ + gfsmstrings_main.c \ + gfsmstrings_cmdparser.c gfsmstrings_cmdparser.h + +gfsmstrings_main.o: gfsmstrings_cmdparser.h + +gfsmstrings_LDFLAGS = $(LDFLAGS_COMMON) +gfsmstrings_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmstrings.gog + + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmunion_SOURCES = \ + gfsmunion_main.c \ + gfsmunion_cmdparser.c gfsmunion_cmdparser.h + +gfsmunion_main.o: gfsmunion_cmdparser.h + +gfsmunion_LDFLAGS = $(LDFLAGS_COMMON) +gfsmunion_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmunion.gog + +##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gfsmviterbi_SOURCES = \ + gfsmviterbi_main.c \ + gfsmviterbi_cmdparser.c gfsmviterbi_cmdparser.h + +gfsmviterbi_main.o: gfsmviterbi_cmdparser.h + +gfsmviterbi_LDFLAGS = $(LDFLAGS_COMMON) +gfsmviterbi_LDADD = $(LDADD_COMMON) + +EXTRA_DIST += gfsmviterbi.gog + +##----------------------------------------------------------------------- +## headers +##----------------------------------------------------------------------- + +## --- installable headers: @includedir@/@pkgname@ +#pkginclude_HEADERS = ${hfile_1} ... ${hfile_N} + +## --- no-install headers +noinst_HEADERS = config.h + +##----------------------------------------------------------------------- +## pre-compile rules: gengetopt +##----------------------------------------------------------------------- + +##----------------------------------------------------------------------- +## pre-compile rules: Getopt::Gen +##----------------------------------------------------------------------- +.SECONDARY: + +OPTGENFLAGS = -u -l --no-handle-rcfile + +if HAVE_OPTGEN + +.gog_cmdparser.h: + optgen.perl $(OPTGENFLAGS) --nopod -F $*_cmdparser $< + +.gog_cmdparser.c: + optgen.perl $(OPTGENFLAGS) --nopod -F $*_cmdparser $< + +.gog.pod: + optgen.perl $(OPTGENFLAGS) --nocfile --nohfile -F $* $< + +endif + +##----------------------------------------------------------------------- +## compile flags +##----------------------------------------------------------------------- + +## --- preprocessor flags +AM_CPPFLAGS = -I@srcdir@/../libgfsm -I../libgfsm + +## --- compiler flags (c++) +AM_CFLAGS = $(gfsm_WFLAGS) $(gfsm_OFLAGS) + +##----------------------------------------------------------------------- +## linker flags +## + library special handling of configure.in vars: +##----------------------------------------------------------------------- +#${prog_i}_LDFLAGS = -L. -static +#${prog_i}_LDADD = ${non_src_file}.o -lsomelib + +LDFLAGS_COMMON = -L../libgfsm +LDADD_COMMON = ../libgfsm/libgfsm.la @gfsm_LIBS@ + +#${lib_i}_la_LDFLAGS = -L. -version-info ${lib_i_current}:${lib_i_rev}:${lib_i_age} +#${lib_i}_LIBADD = -lotherlib + +##----------------------------------------------------------------------- +## cleanup: fst +##----------------------------------------------------------------------- + +fsmclean: + rm -f *.afsa *.afst *.gfsa *.gfst *.thfsa *.thfst + +##----------------------------------------------------------------------- +## Variables: cleanup +##----------------------------------------------------------------------- + +## --- mostlyclean: built by 'make' & commonly rebuilt +#MOSTLYCLEANFILES = + +## --- clean: built by 'make' +#CLEANFILES = + +## --- distclean: built by 'configure' +#DISTCLEANFILES = + +## -- maintainerclean: built by maintainer / by hand +MAINTAINERCLEANFILES = \ + *~ .*~ Makefile Makefile.in \ + *.man *.1 *.html *.txt *.tex \ + *.afsa *.afst *.gfsa *.gfst *.thfst \ + *.dot *.ps *.vcg + +# *_cmdparser.c *_cmdparser.h + +##----------------------------------------------------------------------- +## Variables: distribution +##----------------------------------------------------------------------- + +## --- extra distribution files +#EXTRA_DIST = + +## --- recursion subdirectories for 'make dist' +#DIST_SUBDIRS = $(SUBDIRS) + +## --- dist-hook: when another 'Makefile.am' is overkill +#DISTHOOK_DIRS = foo +#DISTHOOK_FILES = foo/bar.txt foo/baz.txt +#dist-hook: +# for d in $(DISTHOOK_DIRS); do\ +# mkdir -p $(distdir)/$$d ;\ +# done +# for f in $(DISTHOOK_FILES); do\ +# cp -p $(srcdir)/$$f $(distdir)/$$f ;\ +# done + +##----------------------------------------------------------------------- +## Rules: cleanup +##----------------------------------------------------------------------- +.PHONY: cvsclean + +cvsclean: maintainer-clean ; diff --git a/gfsm/gfsm/src/programs/arith.tfst b/gfsm/gfsm/src/programs/arith.tfst new file mode 100644 index 0000000..d7e7b72 --- /dev/null +++ b/gfsm/gfsm/src/programs/arith.tfst @@ -0,0 +1,9 @@ +0 1 1 1 1 +0 2 2 2 2 +0 3 3 3 -3 +0 4 4 4 inf +0 0 +1 1 +2 2 +3 -3 +4 inf diff --git a/gfsm/gfsm/src/programs/c1.tfst b/gfsm/gfsm/src/programs/c1.tfst new file mode 100644 index 0000000..fef3d10 --- /dev/null +++ b/gfsm/gfsm/src/programs/c1.tfst @@ -0,0 +1,5 @@ +0 0 1 2 +0 1 0 0 +1 1 1 3 +0 +1 diff --git a/gfsm/gfsm/src/programs/c2.lab b/gfsm/gfsm/src/programs/c2.lab new file mode 100644 index 0000000..5288861 --- /dev/null +++ b/gfsm/gfsm/src/programs/c2.lab @@ -0,0 +1,11 @@ +<eps> 0 +a 1 +b 2 +A 3 +B 4 +AA 5 +{A|B} 6 +{AA|B} 7 +{} 8 +[e2] 65533 +[e1] 65534 diff --git a/gfsm/gfsm/src/programs/c2.tfst b/gfsm/gfsm/src/programs/c2.tfst new file mode 100644 index 0000000..c437b71 --- /dev/null +++ b/gfsm/gfsm/src/programs/c2.tfst @@ -0,0 +1,4 @@ +0 0 2 3 +0 1 0 0 +1 1 3 1 +0 diff --git a/gfsm/gfsm/src/programs/c2a.tfst b/gfsm/gfsm/src/programs/c2a.tfst new file mode 100644 index 0000000..d6c5236 --- /dev/null +++ b/gfsm/gfsm/src/programs/c2a.tfst @@ -0,0 +1,10 @@ +0 1 a {A|B} +1 2 <eps> a +2 3 a {AA|B} +3 2 <eps> a +2 4 b {A|B} +4 0 <eps> b +0 5 b {A|B} +5 0 <eps> b +0 +2 diff --git a/gfsm/gfsm/src/programs/c2b.tfst b/gfsm/gfsm/src/programs/c2b.tfst new file mode 100644 index 0000000..b16d00e --- /dev/null +++ b/gfsm/gfsm/src/programs/c2b.tfst @@ -0,0 +1,22 @@ +0 1 <eps> <eps> +0 2 <eps> <eps> +0 3 <eps> <eps> +3 4 {A|B} B +3 4 {AA|B} B +4 1 b <eps> +2 5 {A|B} A +2 5 {AA|B} AA +5 1 a <eps> +3 6 {A|B} B +3 6 {AA|B} B +6 2 b <eps> +2 7 {A|B} A +2 7 {AA|B} AA +7 2 a <eps> +3 8 {A|B} B +3 8 {AA|B} B +8 3 b <eps> +2 9 {A|B} A +2 9 {AA|B} AA +9 3 a <eps> +1 diff --git a/gfsm/gfsm/src/programs/c3a.tfst b/gfsm/gfsm/src/programs/c3a.tfst new file mode 100644 index 0000000..d82ca52 --- /dev/null +++ b/gfsm/gfsm/src/programs/c3a.tfst @@ -0,0 +1,8 @@ +0 1 a a +1 0 <eps> A +0 2 b b +2 0 <eps> B +0 +0 0 <eps> <eps1> +1 1 <eps> <eps1> +2 2 <eps> <eps1> diff --git a/gfsm/gfsm/src/programs/c3b.tfst b/gfsm/gfsm/src/programs/c3b.tfst new file mode 100644 index 0000000..4a4133f --- /dev/null +++ b/gfsm/gfsm/src/programs/c3b.tfst @@ -0,0 +1,26 @@ +0 1 <eps1> <eps> +1 +0 2 <eps1> <eps> +2 4 a A +4 1 A <eps> +2 5 a A +5 2 A <eps> +2 6 a A +6 3 A <eps> +0 3 <eps1> <eps> +3 7 b B +7 1 B <eps> +3 8 b B +8 3 B <eps> +3 9 b B +9 2 B <eps> +0 0 <eps2> <eps> +1 1 <eps2> <eps> +2 2 <eps2> <eps> +3 3 <eps2> <eps> +4 4 <eps2> <eps> +5 5 <eps2> <eps> +6 6 <eps2> <eps> +7 7 <eps2> <eps> +8 8 <eps2> <eps> +9 9 <eps2> <eps> diff --git a/gfsm/gfsm/src/programs/c3filt.tfst b/gfsm/gfsm/src/programs/c3filt.tfst new file mode 100644 index 0000000..34cc982 --- /dev/null +++ b/gfsm/gfsm/src/programs/c3filt.tfst @@ -0,0 +1,20 @@ +0 0 <eps2> <eps1> +0 0 a a +0 0 b b +0 0 A A +0 0 B B +0 +0 1 <eps1> <eps1> +1 1 <eps1> <eps1> +1 0 a a +1 0 b b +1 0 A A +1 0 B B +1 +0 2 <eps2> <eps2> +2 2 <eps2> <eps2> +2 0 a a +2 0 b b +2 0 A A +2 0 B B +2 diff --git a/gfsm/gfsm/src/programs/cat1.tfst b/gfsm/gfsm/src/programs/cat1.tfst new file mode 100644 index 0000000..9a51daf --- /dev/null +++ b/gfsm/gfsm/src/programs/cat1.tfst @@ -0,0 +1,3 @@ +0 1 1 1 1.0 +1 2 2 2 2.0 +2 20 diff --git a/gfsm/gfsm/src/programs/cat2.tfst b/gfsm/gfsm/src/programs/cat2.tfst new file mode 100644 index 0000000..5e9e740 --- /dev/null +++ b/gfsm/gfsm/src/programs/cat2.tfst @@ -0,0 +1,4 @@ +0 1 3 3 30 +0 2 4 4 40 +1 100 +2 200 diff --git a/gfsm/gfsm/src/programs/comp1a.tfst b/gfsm/gfsm/src/programs/comp1a.tfst new file mode 100644 index 0000000..c98130b --- /dev/null +++ b/gfsm/gfsm/src/programs/comp1a.tfst @@ -0,0 +1,4 @@ +0 1 1 0 0 +1 2 0 0 0 +2 3 0 2 0 +3 0 diff --git a/gfsm/gfsm/src/programs/comp1b.tfst b/gfsm/gfsm/src/programs/comp1b.tfst new file mode 100644 index 0000000..7818495 --- /dev/null +++ b/gfsm/gfsm/src/programs/comp1b.tfst @@ -0,0 +1,4 @@ +0 1 2 0 0 +1 2 0 0 0 +2 3 0 3 0 +3 0 diff --git a/gfsm/gfsm/src/programs/comp2.tfst b/gfsm/gfsm/src/programs/comp2.tfst new file mode 100644 index 0000000..5eb2b86 --- /dev/null +++ b/gfsm/gfsm/src/programs/comp2.tfst @@ -0,0 +1,6 @@ +0 1 1 0 0 +1 2 0 0 0 +2 3 0 0 0 +3 4 0 0 0 +4 5 0 3 0 +5 0 diff --git a/gfsm/gfsm/src/programs/comp2a.tfst b/gfsm/gfsm/src/programs/comp2a.tfst new file mode 100644 index 0000000..ed416e9 --- /dev/null +++ b/gfsm/gfsm/src/programs/comp2a.tfst @@ -0,0 +1,2 @@ +0 1 1 2 0 +1 0 diff --git a/gfsm/gfsm/src/programs/comp2b.tfst b/gfsm/gfsm/src/programs/comp2b.tfst new file mode 100644 index 0000000..dbf6c01 --- /dev/null +++ b/gfsm/gfsm/src/programs/comp2b.tfst @@ -0,0 +1,3 @@ +0 1 2 0 0 +1 2 0 3 0 +2 0 diff --git a/gfsm/gfsm/src/programs/complement-in.tfst b/gfsm/gfsm/src/programs/complement-in.tfst new file mode 100644 index 0000000..45e1e1b --- /dev/null +++ b/gfsm/gfsm/src/programs/complement-in.tfst @@ -0,0 +1,4 @@ +0 1 1 1 +1 2 2 2 +2 2 2 2 +2 0 diff --git a/gfsm/gfsm/src/programs/compose-mohri-1.tfst b/gfsm/gfsm/src/programs/compose-mohri-1.tfst new file mode 100644 index 0000000..ebf0277 --- /dev/null +++ b/gfsm/gfsm/src/programs/compose-mohri-1.tfst @@ -0,0 +1,5 @@ +0 1 1 1 +1 2 2 0 +2 3 3 0 +3 4 4 4 +4 diff --git a/gfsm/gfsm/src/programs/compose-mohri-2.tfst b/gfsm/gfsm/src/programs/compose-mohri-2.tfst new file mode 100644 index 0000000..154e266 --- /dev/null +++ b/gfsm/gfsm/src/programs/compose-mohri-2.tfst @@ -0,0 +1,4 @@ +0 1 1 4 +1 2 0 5 +2 3 4 1 +3 diff --git a/gfsm/gfsm/src/programs/config.h b/gfsm/gfsm/src/programs/config.h new file mode 100644 index 0000000..2b1cb3c --- /dev/null +++ b/gfsm/gfsm/src/programs/config.h @@ -0,0 +1,13 @@ +#include <gfsmConfig.h> + +/* Define this for verbose memory debugging */ +//#define GFSM_DEBUG_VERBOSE + +#ifdef GFSM_DEBUG_VERBOSE +# include <gfsmDebug.h> +# define GFSM_INIT gfsm_debug_init(); +# define GFSM_FINISH gfsm_debug_finish(); gfsm_debug_print(); +#else +# define GFSM_INIT +# define GFSM_FINISH +#endif diff --git a/gfsm/gfsm/src/programs/connect-in.tfst b/gfsm/gfsm/src/programs/connect-in.tfst new file mode 100644 index 0000000..f160c6c --- /dev/null +++ b/gfsm/gfsm/src/programs/connect-in.tfst @@ -0,0 +1,7 @@ +0 1 0 1 +0 2 0 2 +1 3 1 3 +1 4 1 4 +2 4 2 4 +2 3 2 3 +3 diff --git a/gfsm/gfsm/src/programs/ctest.tfst b/gfsm/gfsm/src/programs/ctest.tfst new file mode 100644 index 0000000..f160c6c --- /dev/null +++ b/gfsm/gfsm/src/programs/ctest.tfst @@ -0,0 +1,7 @@ +0 1 0 1 +0 2 0 2 +1 3 1 3 +1 4 1 4 +2 4 2 4 +2 3 2 3 +3 diff --git a/gfsm/gfsm/src/programs/ctest2.tfst b/gfsm/gfsm/src/programs/ctest2.tfst new file mode 100644 index 0000000..b27f50a --- /dev/null +++ b/gfsm/gfsm/src/programs/ctest2.tfst @@ -0,0 +1,6 @@ +0 1 0 1 +0 2 0 2 +1 3 1 3 +1 4 1 4 +2 4 2 4 +2 3 2 3 diff --git a/gfsm/gfsm/src/programs/det.tfst b/gfsm/gfsm/src/programs/det.tfst new file mode 100644 index 0000000..f93e247 --- /dev/null +++ b/gfsm/gfsm/src/programs/det.tfst @@ -0,0 +1,5 @@ +0 1 1 1 +0 2 1 2 +2 3 3 3 +1 +3 diff --git a/gfsm/gfsm/src/programs/determinize-in.tfst b/gfsm/gfsm/src/programs/determinize-in.tfst new file mode 100644 index 0000000..8a6d556 --- /dev/null +++ b/gfsm/gfsm/src/programs/determinize-in.tfst @@ -0,0 +1,7 @@ +0 1 1 1 +0 2 2 2 +0 2 1 1 +2 1 1 1 +2 3 3 3 +1 10 +3 30 diff --git a/gfsm/gfsm/src/programs/elookup.tfst b/gfsm/gfsm/src/programs/elookup.tfst new file mode 100644 index 0000000..a7a0db1 --- /dev/null +++ b/gfsm/gfsm/src/programs/elookup.tfst @@ -0,0 +1,2 @@ +0 0 0 1 0 +0 0 diff --git a/gfsm/gfsm/src/programs/gfsmarcsort.gog b/gfsm/gfsm/src/programs/gfsmarcsort.gog new file mode 100644 index 0000000..ab827d6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarcsort.gog @@ -0,0 +1,107 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmarcsort" +#program_version "0.01" + +purpose "Sort arcs of a finite state machine" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +flag "lower" l "Sort by (lower,upper,target) [default]." +flag - i "Alias for -l" + +flag "upper" u "Sort by (upper,lower,target)." +flag - o "Alias for '-u'" + +flag "weight" w "Sort by weight (ascending)." +flag "cost" C "Alias for '-w'" + +string "mode" m "Sort by explicit mode string (overrides -l, -u, etc.)" \ + arg="MODE" \ + default="" \ + details=" +Sort by a mode string over the characters \"luwstLUWST_\", as accepted +by gfsm_acmask_from_chars(). Each character in the string represents +a single elementary arc field comparison, and the comparison with the +highest priority should occur leftmost in the string. Correspondence +between characters in MODE and arc comparisons is: + + CHAR : COMPARISON + l : sort by lower labels in ascending order + u : sort by upper labels in ascending order + w : sort by arc weight in semiring-ascending order + s : sort by source state in ascending order (useless here) + t : sort by target state in ascending order + + L : sort by lower labels in descending order + U : sort by upper labels in descending order + W : sort by arc weight in semiring-descending order + S : sort by source state in descending order (useless here) + T : sort by target state in descending order + + _ : no comparison at the corresponding priority level + x : user-specified comparison (useless here) + +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmarcsort_cmdparser.c b/gfsm/gfsm/src/programs/gfsmarcsort_cmdparser.c new file mode 100644 index 0000000..94f4f16 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarcsort_cmdparser.c @@ -0,0 +1,595 @@ +/* -*- Mode: C -*- + * + * File: gfsmarcsort_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.06 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmarcsort_cmdparser gfsmarcsort.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmarcsort" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmarcsort_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmarcsort (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Sort arcs of a finite state machine\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmarcsort"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -l --lower Sort by (lower,upper,target) [default].\n"); + printf(" -i Alias for -l\n"); + printf(" -u --upper Sort by (upper,lower,target).\n"); + printf(" -o Alias for '-u'\n"); + printf(" -w --weight Sort by weight (ascending).\n"); + printf(" -C --cost Alias for '-w'\n"); + printf(" -mMODE --mode=MODE Sort by explicit mode string (overrides -l, -u, etc.)\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->lower_flag = 0; + args_info->i_flag = 0; + args_info->upper_flag = 0; + args_info->o_flag = 0; + args_info->weight_flag = 0; + args_info->cost_flag = 0; + args_info->mode_arg = strdup(""); + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->lower_given = 0; + args_info->i_given = 0; + args_info->upper_given = 0; + args_info->o_given = 0; + args_info->weight_given = 0; + args_info->cost_given = 0; + args_info->mode_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "lower", 0, NULL, 'l' }, + { "upper", 0, NULL, 'u' }, + { "weight", 0, NULL, 'w' }, + { "cost", 0, NULL, 'C' }, + { "mode", 1, NULL, 'm' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'l', + 'i', + 'u', + 'o', + 'w', + 'C', + 'm', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'l': /* Sort by (lower,upper,target) [default]. */ + if (args_info->lower_given) { + fprintf(stderr, "%s: `--lower' (`-l') option given more than once\n", PROGRAM); + } + args_info->lower_given++; + if (args_info->lower_given <= 1) + args_info->lower_flag = !(args_info->lower_flag); + break; + + case 'i': /* Alias for -l */ + if (args_info->i_given) { + fprintf(stderr, "%s: (`-i') option given more than once\n", PROGRAM); + } + args_info->i_given++; + if (args_info->i_given <= 1) + args_info->i_flag = !(args_info->i_flag); + break; + + case 'u': /* Sort by (upper,lower,target). */ + if (args_info->upper_given) { + fprintf(stderr, "%s: `--upper' (`-u') option given more than once\n", PROGRAM); + } + args_info->upper_given++; + if (args_info->upper_given <= 1) + args_info->upper_flag = !(args_info->upper_flag); + break; + + case 'o': /* Alias for '-u' */ + if (args_info->o_given) { + fprintf(stderr, "%s: (`-o') option given more than once\n", PROGRAM); + } + args_info->o_given++; + if (args_info->o_given <= 1) + args_info->o_flag = !(args_info->o_flag); + break; + + case 'w': /* Sort by weight (ascending). */ + if (args_info->weight_given) { + fprintf(stderr, "%s: `--weight' (`-w') option given more than once\n", PROGRAM); + } + args_info->weight_given++; + if (args_info->weight_given <= 1) + args_info->weight_flag = !(args_info->weight_flag); + break; + + case 'C': /* Alias for '-w' */ + if (args_info->cost_given) { + fprintf(stderr, "%s: `--cost' (`-C') option given more than once\n", PROGRAM); + } + args_info->cost_given++; + if (args_info->cost_given <= 1) + args_info->cost_flag = !(args_info->cost_flag); + break; + + case 'm': /* Sort by explicit mode string (overrides -l, -u, etc.) */ + if (args_info->mode_given) { + fprintf(stderr, "%s: `--mode' (`-m') option given more than once\n", PROGRAM); + } + args_info->mode_given++; + if (args_info->mode_arg) free(args_info->mode_arg); + args_info->mode_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Sort by (lower,upper,target) [default]. */ + else if (strcmp(olong, "lower") == 0) { + if (args_info->lower_given) { + fprintf(stderr, "%s: `--lower' (`-l') option given more than once\n", PROGRAM); + } + args_info->lower_given++; + if (args_info->lower_given <= 1) + args_info->lower_flag = !(args_info->lower_flag); + } + + /* Sort by (upper,lower,target). */ + else if (strcmp(olong, "upper") == 0) { + if (args_info->upper_given) { + fprintf(stderr, "%s: `--upper' (`-u') option given more than once\n", PROGRAM); + } + args_info->upper_given++; + if (args_info->upper_given <= 1) + args_info->upper_flag = !(args_info->upper_flag); + } + + /* Sort by weight (ascending). */ + else if (strcmp(olong, "weight") == 0) { + if (args_info->weight_given) { + fprintf(stderr, "%s: `--weight' (`-w') option given more than once\n", PROGRAM); + } + args_info->weight_given++; + if (args_info->weight_given <= 1) + args_info->weight_flag = !(args_info->weight_flag); + } + + /* Alias for '-w' */ + else if (strcmp(olong, "cost") == 0) { + if (args_info->cost_given) { + fprintf(stderr, "%s: `--cost' (`-C') option given more than once\n", PROGRAM); + } + args_info->cost_given++; + if (args_info->cost_given <= 1) + args_info->cost_flag = !(args_info->cost_flag); + } + + /* Sort by explicit mode string (overrides -l, -u, etc.) */ + else if (strcmp(olong, "mode") == 0) { + if (args_info->mode_given) { + fprintf(stderr, "%s: `--mode' (`-m') option given more than once\n", PROGRAM); + } + args_info->mode_given++; + if (args_info->mode_arg) free(args_info->mode_arg); + args_info->mode_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmarcsort_cmdparser.h b/gfsm/gfsm/src/programs/gfsmarcsort_cmdparser.h new file mode 100644 index 0000000..29f5f6a --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarcsort_cmdparser.h @@ -0,0 +1,77 @@ +/* -*- Mode: C -*- + * + * File: gfsmarcsort_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.06. + * + */ + +#ifndef gfsmarcsort_cmdparser_h +#define gfsmarcsort_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int lower_flag; /* Sort by (lower,upper,target) [default]. (default=0). */ + int i_flag; /* Alias for -l (default=0). */ + int upper_flag; /* Sort by (upper,lower,target). (default=0). */ + int o_flag; /* Alias for '-u' (default=0). */ + int weight_flag; /* Sort by weight (ascending). (default=0). */ + int cost_flag; /* Alias for '-w' (default=0). */ + char * mode_arg; /* Sort by explicit mode string (overrides -l, -u, etc.) (default=). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int lower_given; /* Whether lower was given */ + int i_given; /* Whether - was given */ + int upper_given; /* Whether upper was given */ + int o_given; /* Whether - was given */ + int weight_given; /* Whether weight was given */ + int cost_given; /* Whether cost was given */ + int mode_given; /* Whether mode was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmarcsort_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmarcsort_main.c b/gfsm/gfsm/src/programs/gfsmarcsort_main.c new file mode 100644 index 0000000..ac3e48f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarcsort_main.c @@ -0,0 +1,102 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004-2007 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmarcsort_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmarcsort"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs & vars +gfsmAutomaton *fsm; +gfsmArcCompMask mode = gfsmASMLower; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- mode? + if (args.mode_given) { mode = gfsm_acmask_from_chars(args.mode_arg); } + else if (args.lower_given || args.i_given) mode = gfsmASMLower; + else if (args.upper_given || args.o_given) mode = gfsmASMUpper; + else if (args.weight_given || args.cost_given) mode = gfsmASMWeight; + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- sort + gfsm_automaton_arcsort(fsm,mode); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmarith.gog b/gfsm/gfsm/src/programs/gfsmarith.gog new file mode 100644 index 0000000..b2f14b9 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarith.gog @@ -0,0 +1,132 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmarith" +#program_version "0.01" + +purpose "Perform an elementary arithmetic operation on automaton weights" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "FSMFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- + +#------------------------------------------------------- +# Basic Options + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#------------------------------------------------------- +# Operator Selection +group "Operator Selection" + +flag "exp" E "Real exponentiation" + +flag "log" L "Real logarithm" + +float "multiply" m "Real multiplication by WEIGHT" \ + arg="WEIGHT" + +float "add" a "Real addition of WEIGHT" \ + arg="WEIGHT" + +flag "positive" p "Set real negative weights to zero" + + +float "times" - "Semiring multiplication by WEIGHT" \ + arg="WEIGHT" + +float "plus" - "Semiring addition of WEIGHT" \ + arg="WEIGHT" + +flag "sr-positive" - "Set semiring-negative weights to semiring-zero" + +#------------------------------------------------------- +# Weight Selection +group "Weight Selection" + +flag "no-arcs" A "Exclude arc weights (default: include)" + +flag "no-finals" f "Exclude final weights (default: include)" + +#flag "potentials" P "Include potentials (default: include)" ##-- what the heck are 'potentials'? + +flag "zero" Z "Include semiring-zero weights (default: exclude)" + +#------------------------------------------------------- +# State & Label Selection +group "State & Label Selection" + +flag "initial" i "Select only initial state and its arcs/finality" + +int "state" s "Select only state ID and its arcs/finality (default=all)" \ + arg="ID" \ + default="-1" + +int "lower" l "Select only arcs with lower-label LO (default=any)" \ + arg="LO" \ + default="-1" + +int "upper" u "Select only arcs with upper-label HI (defualt=any)" \ + arg="HI" \ + default="-1" + + + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +Note that the --plus and --times operations are +sensitive to the semiring associated with the stored FSM file, while +the --add and --multiply operations have the usual real-number interpretations, +regardless of the automaton semiring. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmarith_cmdparser.c b/gfsm/gfsm/src/programs/gfsmarith_cmdparser.c new file mode 100644 index 0000000..7aade2f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarith_cmdparser.c @@ -0,0 +1,773 @@ +/* -*- Mode: C -*- + * + * File: gfsmarith_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmarith_cmdparser gfsmarith.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmarith" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmarith_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmarith (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Perform an elementary arithmetic operation on automaton weights\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... FSMFILE\n", "gfsmarith"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" FSMFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); + printf("\n"); + printf(" Operator Selection:\n"); + printf(" -E --exp Real exponentiation\n"); + printf(" -L --log Real logarithm\n"); + printf(" -mFLOAT --multiply=FLOAT Real multiplication by WEIGHT\n"); + printf(" -aFLOAT --add=FLOAT Real addition of WEIGHT\n"); + printf(" -p --positive Set real negative weights to zero\n"); + printf(" --times=FLOAT Semiring multiplication by WEIGHT\n"); + printf(" --plus=FLOAT Semiring addition of WEIGHT\n"); + printf(" --sr-positive Set semiring-negative weights to semiring-zero\n"); + printf("\n"); + printf(" Weight Selection:\n"); + printf(" -A --no-arcs Exclude arc weights (default: include)\n"); + printf(" -f --no-finals Exclude final weights (default: include)\n"); + printf(" -Z --zero Include semiring-zero weights (default: exclude)\n"); + printf("\n"); + printf(" State & Label Selection:\n"); + printf(" -i --initial Select only initial state and its arcs/finality\n"); + printf(" -sID --state=ID Select only state ID and its arcs/finality (default=all)\n"); + printf(" -lLO --lower=LO Select only arcs with lower-label LO (default=any)\n"); + printf(" -uHI --upper=HI Select only arcs with upper-label HI (defualt=any)\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); + args_info->exp_flag = 0; + args_info->log_flag = 0; + args_info->multiply_arg = 0; + args_info->add_arg = 0; + args_info->positive_flag = 0; + args_info->times_arg = 0; + args_info->plus_arg = 0; + args_info->sr_positive_flag = 0; + args_info->no_arcs_flag = 0; + args_info->no_finals_flag = 0; + args_info->zero_flag = 0; + args_info->initial_flag = 0; + args_info->state_arg = -1; + args_info->lower_arg = -1; + args_info->upper_arg = -1; +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + args_info->exp_given = 0; + args_info->log_given = 0; + args_info->multiply_given = 0; + args_info->add_given = 0; + args_info->positive_given = 0; + args_info->times_given = 0; + args_info->plus_given = 0; + args_info->sr_positive_given = 0; + args_info->no_arcs_given = 0; + args_info->no_finals_given = 0; + args_info->zero_given = 0; + args_info->initial_given = 0; + args_info->state_given = 0; + args_info->lower_given = 0; + args_info->upper_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { "exp", 0, NULL, 'E' }, + { "log", 0, NULL, 'L' }, + { "multiply", 1, NULL, 'm' }, + { "add", 1, NULL, 'a' }, + { "positive", 0, NULL, 'p' }, + { "times", 1, NULL, 0 }, + { "plus", 1, NULL, 0 }, + { "sr-positive", 0, NULL, 0 }, + { "no-arcs", 0, NULL, 'A' }, + { "no-finals", 0, NULL, 'f' }, + { "zero", 0, NULL, 'Z' }, + { "initial", 0, NULL, 'i' }, + { "state", 1, NULL, 's' }, + { "lower", 1, NULL, 'l' }, + { "upper", 1, NULL, 'u' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + 'E', + 'L', + 'm', ':', + 'a', ':', + 'p', + 'A', + 'f', + 'Z', + 'i', + 's', ':', + 'l', ':', + 'u', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 'E': /* Real exponentiation */ + if (args_info->exp_given) { + fprintf(stderr, "%s: `--exp' (`-E') option given more than once\n", PROGRAM); + } + args_info->exp_given++; + if (args_info->exp_given <= 1) + args_info->exp_flag = !(args_info->exp_flag); + break; + + case 'L': /* Real logarithm */ + if (args_info->log_given) { + fprintf(stderr, "%s: `--log' (`-L') option given more than once\n", PROGRAM); + } + args_info->log_given++; + if (args_info->log_given <= 1) + args_info->log_flag = !(args_info->log_flag); + break; + + case 'm': /* Real multiplication by WEIGHT */ + if (args_info->multiply_given) { + fprintf(stderr, "%s: `--multiply' (`-m') option given more than once\n", PROGRAM); + } + args_info->multiply_given++; + args_info->multiply_arg = (float)strtod(val, NULL); + break; + + case 'a': /* Real addition of WEIGHT */ + if (args_info->add_given) { + fprintf(stderr, "%s: `--add' (`-a') option given more than once\n", PROGRAM); + } + args_info->add_given++; + args_info->add_arg = (float)strtod(val, NULL); + break; + + case 'p': /* Set real negative weights to zero */ + if (args_info->positive_given) { + fprintf(stderr, "%s: `--positive' (`-p') option given more than once\n", PROGRAM); + } + args_info->positive_given++; + if (args_info->positive_given <= 1) + args_info->positive_flag = !(args_info->positive_flag); + break; + + case 'A': /* Exclude arc weights (default: include) */ + if (args_info->no_arcs_given) { + fprintf(stderr, "%s: `--no-arcs' (`-A') option given more than once\n", PROGRAM); + } + args_info->no_arcs_given++; + if (args_info->no_arcs_given <= 1) + args_info->no_arcs_flag = !(args_info->no_arcs_flag); + break; + + case 'f': /* Exclude final weights (default: include) */ + if (args_info->no_finals_given) { + fprintf(stderr, "%s: `--no-finals' (`-f') option given more than once\n", PROGRAM); + } + args_info->no_finals_given++; + if (args_info->no_finals_given <= 1) + args_info->no_finals_flag = !(args_info->no_finals_flag); + break; + + case 'Z': /* Include semiring-zero weights (default: exclude) */ + if (args_info->zero_given) { + fprintf(stderr, "%s: `--zero' (`-Z') option given more than once\n", PROGRAM); + } + args_info->zero_given++; + if (args_info->zero_given <= 1) + args_info->zero_flag = !(args_info->zero_flag); + break; + + case 'i': /* Select only initial state and its arcs/finality */ + if (args_info->initial_given) { + fprintf(stderr, "%s: `--initial' (`-i') option given more than once\n", PROGRAM); + } + args_info->initial_given++; + if (args_info->initial_given <= 1) + args_info->initial_flag = !(args_info->initial_flag); + break; + + case 's': /* Select only state ID and its arcs/finality (default=all) */ + if (args_info->state_given) { + fprintf(stderr, "%s: `--state' (`-s') option given more than once\n", PROGRAM); + } + args_info->state_given++; + args_info->state_arg = (int)atoi(val); + break; + + case 'l': /* Select only arcs with lower-label LO (default=any) */ + if (args_info->lower_given) { + fprintf(stderr, "%s: `--lower' (`-l') option given more than once\n", PROGRAM); + } + args_info->lower_given++; + args_info->lower_arg = (int)atoi(val); + break; + + case 'u': /* Select only arcs with upper-label HI (defualt=any) */ + if (args_info->upper_given) { + fprintf(stderr, "%s: `--upper' (`-u') option given more than once\n", PROGRAM); + } + args_info->upper_given++; + args_info->upper_arg = (int)atoi(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + /* Real exponentiation */ + else if (strcmp(olong, "exp") == 0) { + if (args_info->exp_given) { + fprintf(stderr, "%s: `--exp' (`-E') option given more than once\n", PROGRAM); + } + args_info->exp_given++; + if (args_info->exp_given <= 1) + args_info->exp_flag = !(args_info->exp_flag); + } + + /* Real logarithm */ + else if (strcmp(olong, "log") == 0) { + if (args_info->log_given) { + fprintf(stderr, "%s: `--log' (`-L') option given more than once\n", PROGRAM); + } + args_info->log_given++; + if (args_info->log_given <= 1) + args_info->log_flag = !(args_info->log_flag); + } + + /* Real multiplication by WEIGHT */ + else if (strcmp(olong, "multiply") == 0) { + if (args_info->multiply_given) { + fprintf(stderr, "%s: `--multiply' (`-m') option given more than once\n", PROGRAM); + } + args_info->multiply_given++; + args_info->multiply_arg = (float)strtod(val, NULL); + } + + /* Real addition of WEIGHT */ + else if (strcmp(olong, "add") == 0) { + if (args_info->add_given) { + fprintf(stderr, "%s: `--add' (`-a') option given more than once\n", PROGRAM); + } + args_info->add_given++; + args_info->add_arg = (float)strtod(val, NULL); + } + + /* Set real negative weights to zero */ + else if (strcmp(olong, "positive") == 0) { + if (args_info->positive_given) { + fprintf(stderr, "%s: `--positive' (`-p') option given more than once\n", PROGRAM); + } + args_info->positive_given++; + if (args_info->positive_given <= 1) + args_info->positive_flag = !(args_info->positive_flag); + } + + /* Semiring multiplication by WEIGHT */ + else if (strcmp(olong, "times") == 0) { + if (args_info->times_given) { + fprintf(stderr, "%s: `--times' option given more than once\n", PROGRAM); + } + args_info->times_given++; + args_info->times_arg = (float)strtod(val, NULL); + } + + /* Semiring addition of WEIGHT */ + else if (strcmp(olong, "plus") == 0) { + if (args_info->plus_given) { + fprintf(stderr, "%s: `--plus' option given more than once\n", PROGRAM); + } + args_info->plus_given++; + args_info->plus_arg = (float)strtod(val, NULL); + } + + /* Set semiring-negative weights to semiring-zero */ + else if (strcmp(olong, "sr-positive") == 0) { + if (args_info->sr_positive_given) { + fprintf(stderr, "%s: `--sr-positive' option given more than once\n", PROGRAM); + } + args_info->sr_positive_given++; + if (args_info->sr_positive_given <= 1) + args_info->sr_positive_flag = !(args_info->sr_positive_flag); + } + + /* Exclude arc weights (default: include) */ + else if (strcmp(olong, "no-arcs") == 0) { + if (args_info->no_arcs_given) { + fprintf(stderr, "%s: `--no-arcs' (`-A') option given more than once\n", PROGRAM); + } + args_info->no_arcs_given++; + if (args_info->no_arcs_given <= 1) + args_info->no_arcs_flag = !(args_info->no_arcs_flag); + } + + /* Exclude final weights (default: include) */ + else if (strcmp(olong, "no-finals") == 0) { + if (args_info->no_finals_given) { + fprintf(stderr, "%s: `--no-finals' (`-f') option given more than once\n", PROGRAM); + } + args_info->no_finals_given++; + if (args_info->no_finals_given <= 1) + args_info->no_finals_flag = !(args_info->no_finals_flag); + } + + /* Include semiring-zero weights (default: exclude) */ + else if (strcmp(olong, "zero") == 0) { + if (args_info->zero_given) { + fprintf(stderr, "%s: `--zero' (`-Z') option given more than once\n", PROGRAM); + } + args_info->zero_given++; + if (args_info->zero_given <= 1) + args_info->zero_flag = !(args_info->zero_flag); + } + + /* Select only initial state and its arcs/finality */ + else if (strcmp(olong, "initial") == 0) { + if (args_info->initial_given) { + fprintf(stderr, "%s: `--initial' (`-i') option given more than once\n", PROGRAM); + } + args_info->initial_given++; + if (args_info->initial_given <= 1) + args_info->initial_flag = !(args_info->initial_flag); + } + + /* Select only state ID and its arcs/finality (default=all) */ + else if (strcmp(olong, "state") == 0) { + if (args_info->state_given) { + fprintf(stderr, "%s: `--state' (`-s') option given more than once\n", PROGRAM); + } + args_info->state_given++; + args_info->state_arg = (int)atoi(val); + } + + /* Select only arcs with lower-label LO (default=any) */ + else if (strcmp(olong, "lower") == 0) { + if (args_info->lower_given) { + fprintf(stderr, "%s: `--lower' (`-l') option given more than once\n", PROGRAM); + } + args_info->lower_given++; + args_info->lower_arg = (int)atoi(val); + } + + /* Select only arcs with upper-label HI (defualt=any) */ + else if (strcmp(olong, "upper") == 0) { + if (args_info->upper_given) { + fprintf(stderr, "%s: `--upper' (`-u') option given more than once\n", PROGRAM); + } + args_info->upper_given++; + args_info->upper_arg = (int)atoi(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmarith_cmdparser.h b/gfsm/gfsm/src/programs/gfsmarith_cmdparser.h new file mode 100644 index 0000000..0f8b5c4 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarith_cmdparser.h @@ -0,0 +1,93 @@ +/* -*- Mode: C -*- + * + * File: gfsmarith_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmarith_cmdparser_h +#define gfsmarith_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + int exp_flag; /* Real exponentiation (default=0). */ + int log_flag; /* Real logarithm (default=0). */ + float multiply_arg; /* Real multiplication by WEIGHT (default=0). */ + float add_arg; /* Real addition of WEIGHT (default=0). */ + int positive_flag; /* Set real negative weights to zero (default=0). */ + float times_arg; /* Semiring multiplication by WEIGHT (default=0). */ + float plus_arg; /* Semiring addition of WEIGHT (default=0). */ + int sr_positive_flag; /* Set semiring-negative weights to semiring-zero (default=0). */ + int no_arcs_flag; /* Exclude arc weights (default: include) (default=0). */ + int no_finals_flag; /* Exclude final weights (default: include) (default=0). */ + int zero_flag; /* Include semiring-zero weights (default: exclude) (default=0). */ + int initial_flag; /* Select only initial state and its arcs/finality (default=0). */ + int state_arg; /* Select only state ID and its arcs/finality (default=all) (default=-1). */ + int lower_arg; /* Select only arcs with lower-label LO (default=any) (default=-1). */ + int upper_arg; /* Select only arcs with upper-label HI (defualt=any) (default=-1). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + int exp_given; /* Whether exp was given */ + int log_given; /* Whether log was given */ + int multiply_given; /* Whether multiply was given */ + int add_given; /* Whether add was given */ + int positive_given; /* Whether positive was given */ + int times_given; /* Whether times was given */ + int plus_given; /* Whether plus was given */ + int sr_positive_given; /* Whether sr-positive was given */ + int no_arcs_given; /* Whether no-arcs was given */ + int no_finals_given; /* Whether no-finals was given */ + int zero_given; /* Whether zero was given */ + int initial_given; /* Whether initial was given */ + int state_given; /* Whether state was given */ + int lower_given; /* Whether lower was given */ + int upper_given; /* Whether upper was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmarith_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmarith_main.c b/gfsm/gfsm/src/programs/gfsmarith_main.c new file mode 100644 index 0000000..53f5708 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmarith_main.c @@ -0,0 +1,132 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <gfsm.h> + +#include "gfsmarith_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmarith"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; +gfsmError *err = NULL; + +//-- arithmetic operation +gfsmArithOp op=gfsmAONone; +gfsmWeight arg=0; + +//-- weight selection +gboolean do_arcs; +gboolean do_finals; +gboolean do_zero; + +//-- state & label selection +gfsmStateId qid; +gfsmLabelVal lo; +gfsmLabelVal hi; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + outfilename = args.output_arg; + + //-- operator selection + if (args.exp_given) { op = gfsmAOExp; } + else if (args.log_given) { op = gfsmAOLog; } + else if (args.multiply_given) { op = gfsmAOMult; arg=args.multiply_arg; } + else if (args.add_given) { op = gfsmAOAdd; arg=args.add_arg; } + else if (args.positive_given) { op = gfsmAONoNeg; } + else if (args.times_given) { op = gfsmAOSRTimes; arg=args.times_arg; } + else if (args.plus_given) { op = gfsmAOSRPlus; arg=args.plus_arg; } + else if (args.sr_positive_given) { op = gfsmAOSRNoNeg; } + + //-- weight selection + do_arcs = !args.no_arcs_given; + do_finals = !args.no_finals_given; + do_zero = args.zero_given; + + //-- state & label selection + qid = args.state_given ? args.state_arg : gfsmNoState; + lo = args.lower_given ? args.lower_arg : gfsmNoLabel; + hi = args.upper_given ? args.upper_arg : gfsmNoLabel; + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(3); + } + + //-- hack: initial-state selection + if (args.initial_flag) qid=fsm->root_id; + + //-- perform weight aritmetic + gfsm_automaton_arith_state(fsm, qid, op, arg, lo, hi, do_arcs, do_finals, do_zero); + + //-- store automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + gfsm_automaton_free(fsm); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmclosure.gog b/gfsm/gfsm/src/programs/gfsmclosure.gog new file mode 100644 index 0000000..77b5884 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmclosure.gog @@ -0,0 +1,79 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmclosure" +#program_version "0.01" + +purpose "Compute Kleene closure of a finite state machine" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "times" n "Compute n-ary (^n) closure." \ + arg="N" \ + default="0" + +flag "plus" p "Compute transitive (+) closure." \ + details=" +Alias for '-n 1'. +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmclosure_cmdparser.c b/gfsm/gfsm/src/programs/gfsmclosure_cmdparser.c new file mode 100644 index 0000000..11f7bfd --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmclosure_cmdparser.c @@ -0,0 +1,495 @@ +/* -*- Mode: C -*- + * + * File: gfsmclosure_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmclosure_cmdparser gfsmclosure.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmclosure" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmclosure_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmclosure (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute Kleene closure of a finite state machine\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmclosure"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -nN --times=N Compute n-ary (^n) closure.\n"); + printf(" -p --plus Compute transitive (+) closure.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->times_arg = 0; + args_info->plus_flag = 0; + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->times_given = 0; + args_info->plus_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "times", 1, NULL, 'n' }, + { "plus", 0, NULL, 'p' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'n', ':', + 'p', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'n': /* Compute n-ary (^n) closure. */ + if (args_info->times_given) { + fprintf(stderr, "%s: `--times' (`-n') option given more than once\n", PROGRAM); + } + args_info->times_given++; + args_info->times_arg = (int)atoi(val); + break; + + case 'p': /* Compute transitive (+) closure. */ + if (args_info->plus_given) { + fprintf(stderr, "%s: `--plus' (`-p') option given more than once\n", PROGRAM); + } + args_info->plus_given++; + if (args_info->plus_given <= 1) + args_info->plus_flag = !(args_info->plus_flag); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Compute n-ary (^n) closure. */ + else if (strcmp(olong, "times") == 0) { + if (args_info->times_given) { + fprintf(stderr, "%s: `--times' (`-n') option given more than once\n", PROGRAM); + } + args_info->times_given++; + args_info->times_arg = (int)atoi(val); + } + + /* Compute transitive (+) closure. */ + else if (strcmp(olong, "plus") == 0) { + if (args_info->plus_given) { + fprintf(stderr, "%s: `--plus' (`-p') option given more than once\n", PROGRAM); + } + args_info->plus_given++; + if (args_info->plus_given <= 1) + args_info->plus_flag = !(args_info->plus_flag); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmclosure_cmdparser.h b/gfsm/gfsm/src/programs/gfsmclosure_cmdparser.h new file mode 100644 index 0000000..0837b6a --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmclosure_cmdparser.h @@ -0,0 +1,67 @@ +/* -*- Mode: C -*- + * + * File: gfsmclosure_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmclosure_cmdparser_h +#define gfsmclosure_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int times_arg; /* Compute n-ary (^n) closure. (default=0). */ + int plus_flag; /* Compute transitive (+) closure. (default=0). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int times_given; /* Whether times was given */ + int plus_given; /* Whether plus was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmclosure_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmclosure_main.c b/gfsm/gfsm/src/programs/gfsmclosure_main.c new file mode 100644 index 0000000..8b8d029 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmclosure_main.c @@ -0,0 +1,100 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmclosure_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmclosure"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsm=NULL; +guint times = 0; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- get closure length + if (args.plus_given) times = 1; + else times = args.times_arg; + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute closure + gfsm_automaton_n_closure(fsm, times); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmcompile.gog b/gfsm/gfsm/src/programs/gfsmcompile.gog new file mode 100644 index 0000000..e53307b --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompile.gog @@ -0,0 +1,102 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmcompile" +#program_version "0.01" + +purpose "Compile text format gfsm files to binary" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "TXTFILE" "Input text file" \ + details=" +If unspecified, standard input will be read +" + +#argument "BINFILE" "Output binary gfsm file" \ +# details=" +#If unspecified, output will be written to standard output. +#" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +flag "acceptor" a "Compile as acceptor (default=transducer)" \ + default=0 + +string "ilabels" i "Specify input (lower) labels file." \ + arg="LABELS" + +string "olabels" o "Specify output (upper) labels file." \ + arg="LABELS" + +string "slabels" S "Specify state labels file." \ + arg="LABELS" + +string "semiring" s "Specify semiring type." \ + arg="SRTYPE" \ + default="tropical" \ + details=" +Specify one of the following: + + boolean + log + plog + real + trivial + tropical + +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmcompile_cmdparser.c b/gfsm/gfsm/src/programs/gfsmcompile_cmdparser.c new file mode 100644 index 0000000..ea17d5d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompile_cmdparser.c @@ -0,0 +1,569 @@ +/* -*- Mode: C -*- + * + * File: gfsmcompile_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmcompile_cmdparser gfsmcompile.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmcompile" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmcompile_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmcompile (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compile text format gfsm files to binary\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... TXTFILE\n", "gfsmcompile"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" TXTFILE Input text file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -a --acceptor Compile as acceptor (default=transducer)\n"); + printf(" -iLABELS --ilabels=LABELS Specify input (lower) labels file.\n"); + printf(" -oLABELS --olabels=LABELS Specify output (upper) labels file.\n"); + printf(" -SLABELS --slabels=LABELS Specify state labels file.\n"); + printf(" -sSRTYPE --semiring=SRTYPE Specify semiring type.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->acceptor_flag = 0; + args_info->ilabels_arg = NULL; + args_info->olabels_arg = NULL; + args_info->slabels_arg = NULL; + args_info->semiring_arg = strdup("tropical"); + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->acceptor_given = 0; + args_info->ilabels_given = 0; + args_info->olabels_given = 0; + args_info->slabels_given = 0; + args_info->semiring_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "acceptor", 0, NULL, 'a' }, + { "ilabels", 1, NULL, 'i' }, + { "olabels", 1, NULL, 'o' }, + { "slabels", 1, NULL, 'S' }, + { "semiring", 1, NULL, 's' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'a', + 'i', ':', + 'o', ':', + 'S', ':', + 's', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'a': /* Compile as acceptor (default=transducer) */ + if (args_info->acceptor_given) { + fprintf(stderr, "%s: `--acceptor' (`-a') option given more than once\n", PROGRAM); + } + args_info->acceptor_given++; + if (args_info->acceptor_given <= 1) + args_info->acceptor_flag = !(args_info->acceptor_flag); + break; + + case 'i': /* Specify input (lower) labels file. */ + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + break; + + case 'o': /* Specify output (upper) labels file. */ + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + break; + + case 'S': /* Specify state labels file. */ + if (args_info->slabels_given) { + fprintf(stderr, "%s: `--slabels' (`-S') option given more than once\n", PROGRAM); + } + args_info->slabels_given++; + if (args_info->slabels_arg) free(args_info->slabels_arg); + args_info->slabels_arg = strdup(val); + break; + + case 's': /* Specify semiring type. */ + if (args_info->semiring_given) { + fprintf(stderr, "%s: `--semiring' (`-s') option given more than once\n", PROGRAM); + } + args_info->semiring_given++; + if (args_info->semiring_arg) free(args_info->semiring_arg); + args_info->semiring_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Compile as acceptor (default=transducer) */ + else if (strcmp(olong, "acceptor") == 0) { + if (args_info->acceptor_given) { + fprintf(stderr, "%s: `--acceptor' (`-a') option given more than once\n", PROGRAM); + } + args_info->acceptor_given++; + if (args_info->acceptor_given <= 1) + args_info->acceptor_flag = !(args_info->acceptor_flag); + } + + /* Specify input (lower) labels file. */ + else if (strcmp(olong, "ilabels") == 0) { + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + } + + /* Specify output (upper) labels file. */ + else if (strcmp(olong, "olabels") == 0) { + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + } + + /* Specify state labels file. */ + else if (strcmp(olong, "slabels") == 0) { + if (args_info->slabels_given) { + fprintf(stderr, "%s: `--slabels' (`-S') option given more than once\n", PROGRAM); + } + args_info->slabels_given++; + if (args_info->slabels_arg) free(args_info->slabels_arg); + args_info->slabels_arg = strdup(val); + } + + /* Specify semiring type. */ + else if (strcmp(olong, "semiring") == 0) { + if (args_info->semiring_given) { + fprintf(stderr, "%s: `--semiring' (`-s') option given more than once\n", PROGRAM); + } + args_info->semiring_given++; + if (args_info->semiring_arg) free(args_info->semiring_arg); + args_info->semiring_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmcompile_cmdparser.h b/gfsm/gfsm/src/programs/gfsmcompile_cmdparser.h new file mode 100644 index 0000000..53c4d59 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompile_cmdparser.h @@ -0,0 +1,73 @@ +/* -*- Mode: C -*- + * + * File: gfsmcompile_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmcompile_cmdparser_h +#define gfsmcompile_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int acceptor_flag; /* Compile as acceptor (default=transducer) (default=0). */ + char * ilabels_arg; /* Specify input (lower) labels file. (default=NULL). */ + char * olabels_arg; /* Specify output (upper) labels file. (default=NULL). */ + char * slabels_arg; /* Specify state labels file. (default=NULL). */ + char * semiring_arg; /* Specify semiring type. (default=tropical). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int acceptor_given; /* Whether acceptor was given */ + int ilabels_given; /* Whether ilabels was given */ + int olabels_given; /* Whether olabels was given */ + int slabels_given; /* Whether slabels was given */ + int semiring_given; /* Whether semiring was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmcompile_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmcompile_main.c b/gfsm/gfsm/src/programs/gfsmcompile_main.c new file mode 100644 index 0000000..feeba69 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompile_main.c @@ -0,0 +1,132 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <gfsm.h> + +#include "gfsmcompile_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmcompile"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; +gfsmAlphabet *ilabels=NULL, *olabels=NULL, *slabels=NULL; +gfsmError *err = NULL; +gfsmSRType srtype = gfsmSRTUnknown; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + outfilename = args.output_arg; + + //-- labels: input + if (args.ilabels_given) { + ilabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(ilabels,args.ilabels_arg,&err)) { + g_printerr("%s: load failed for input-labels file '%s': %s\n", + progname, args.ilabels_arg, err->message); + exit(2); + } + } + //-- labels: output + if (args.olabels_given) { + olabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(olabels,args.olabels_arg,&err)) { + g_printerr("%s: load failed for output-labels file '%s': %s\n", + progname, args.olabels_arg, err->message); + exit(2); + } + } + //-- labels: state + if (args.slabels_given) { + slabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(slabels,args.slabels_arg,&err)) { + g_printerr("%s: load failed for state-labels file '%s': %s\n", + progname, args.slabels_arg, err->message); + exit(2); + } + } + + //-- initialize fsm + fsm = gfsm_automaton_new(); + if (args.acceptor_given) fsm->flags.is_transducer = FALSE; + + //-- set semiring + srtype = gfsm_sr_name_to_type(args.semiring_arg); + if (srtype != fsm->sr->type) { + gfsm_automaton_set_semiring(fsm, gfsm_semiring_new(srtype)); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + get_my_options(argc,argv); + + //-- compile automaton + if (!gfsm_automaton_compile_filename_full(fsm,infilename,ilabels,olabels,slabels,&err)) { + g_printerr("%s: compile failed for '%s': %s\n", progname, infilename, err->message); + exit(3); + } + + //-- store automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (ilabels) gfsm_alphabet_free(ilabels); + if (olabels) gfsm_alphabet_free(olabels); + if (slabels) gfsm_alphabet_free(slabels); + gfsm_automaton_free(fsm); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmcomplement.gog b/gfsm/gfsm/src/programs/gfsmcomplement.gog new file mode 100644 index 0000000..7712b3c --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcomplement.gog @@ -0,0 +1,73 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmcomplement" +#program_version "0.01" + +purpose "Compute complement of finite state acceptors" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "ilabels" i "Specify input (lower) labels file for alphabet." \ + arg="LABELS" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +Epsilon arcs are not handled correctly. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmcomplement_cmdparser.c b/gfsm/gfsm/src/programs/gfsmcomplement_cmdparser.c new file mode 100644 index 0000000..d697579 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcomplement_cmdparser.c @@ -0,0 +1,473 @@ +/* -*- Mode: C -*- + * + * File: gfsmcomplement_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmcomplement_cmdparser gfsmcomplement.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmcomplement" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmcomplement_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmcomplement (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute complement of finite state acceptors\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmcomplement"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -iLABELS --ilabels=LABELS Specify input (lower) labels file for alphabet.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->ilabels_arg = NULL; + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->ilabels_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "ilabels", 1, NULL, 'i' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'i', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'i': /* Specify input (lower) labels file for alphabet. */ + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify input (lower) labels file for alphabet. */ + else if (strcmp(olong, "ilabels") == 0) { + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmcomplement_cmdparser.h b/gfsm/gfsm/src/programs/gfsmcomplement_cmdparser.h new file mode 100644 index 0000000..4b3d57d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcomplement_cmdparser.h @@ -0,0 +1,65 @@ +/* -*- Mode: C -*- + * + * File: gfsmcomplement_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmcomplement_cmdparser_h +#define gfsmcomplement_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * ilabels_arg; /* Specify input (lower) labels file for alphabet. (default=NULL). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int ilabels_given; /* Whether ilabels was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmcomplement_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmcomplement_main.c b/gfsm/gfsm/src/programs/gfsmcomplement_main.c new file mode 100644 index 0000000..227b963 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcomplement_main.c @@ -0,0 +1,105 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmcomplement_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmcomplement"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; +gfsmAlphabet *ilabels=NULL; +gfsmError *err=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- labels: input + if (args.ilabels_given) { + ilabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(ilabels,args.ilabels_arg,&err)) { + g_printerr("%s: load failed for input-labels file '%s': %s\n", + progname, args.ilabels_arg, err->message); + exit(2); + } + } + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- complement + if (ilabels) gfsm_automaton_complement_full(fsm,ilabels); + else gfsm_automaton_complement(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (ilabels) gfsm_alphabet_free(ilabels); + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmcompose.gog b/gfsm/gfsm/src/programs/gfsmcompose.gog new file mode 100644 index 0000000..c4e34f3 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompose.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmcompose" +#program_version "0.01" + +purpose "Compute composition of finite-state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE(s)" "Stored binary gfsm file(s)" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +Probably many. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmcompose_cmdparser.c b/gfsm/gfsm/src/programs/gfsmcompose_cmdparser.c new file mode 100644 index 0000000..5a82199 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompose_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmcompose_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmcompose_cmdparser gfsmcompose.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmcompose" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmcompose_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmcompose (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute composition of finite-state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE(s)\n", "gfsmcompose"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE(s) Stored binary gfsm file(s)\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmcompose_cmdparser.h b/gfsm/gfsm/src/programs/gfsmcompose_cmdparser.h new file mode 100644 index 0000000..a99342e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompose_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmcompose_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmcompose_cmdparser_h +#define gfsmcompose_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmcompose_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmcompose_main.c b/gfsm/gfsm/src/programs/gfsmcompose_main.c new file mode 100644 index 0000000..232c5e6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompose_main.c @@ -0,0 +1,123 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005-2007 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmcompose_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmcompose"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmOut=NULL, *fsmIn=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- require at least one file argument + if (args.inputs_num < 1) { + cmdline_parser_print_help(); + exit(2); + } + + //-- output + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsmIn = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * compute_operation() + * + utility routine + */ +void compute_operation(const char *infilename) +{ + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsmIn,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute underlying FSM operation + if (fsmOut == NULL) { + fsmOut = fsmIn; + fsmIn = gfsm_automaton_new(); + } else { + gfsm_automaton_compose(fsmOut,fsmIn); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + + GFSM_INIT + + get_my_options(argc,argv); + + for (i = 0; i < args.inputs_num; i++) { + compute_operation(args.inputs[i]); + } + if (args.inputs_num == 1) compute_operation("-"); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmOut,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsmIn) gfsm_automaton_free(fsmIn); + if (fsmOut) gfsm_automaton_free(fsmOut); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmcompre.gog b/gfsm/gfsm/src/programs/gfsmcompre.gog new file mode 100644 index 0000000..7e4058f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompre.gog @@ -0,0 +1,94 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmcompre" +#program_version "0.01" + +purpose "Compile an automaton from a regular expression" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +#argument "LABELID..." "Input label IDs" \ +# details=" +#In ASCII decimal notation. +#" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "labels" l "Basic label alphabet" \ + arg="LABFILE" + +string "regex" s "Regular expression to compile" \ + arg="REGEX" + +flag "string" x "Compile as string, not regex" \ + default=0 + +string "semiring" r "Specify semiring type" \ + arg="SRTYPE" \ + default="tropical" \ + details=" +Specify one of the following: + + boolean + log + plog + real + trivial + tropical + +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmcompre_cmdparser.c b/gfsm/gfsm/src/programs/gfsmcompre_cmdparser.c new file mode 100644 index 0000000..75f5810 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompre_cmdparser.c @@ -0,0 +1,542 @@ +/* -*- Mode: C -*- + * + * File: gfsmcompre_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmcompre_cmdparser gfsmcompre.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmcompre" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmcompre_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmcompre (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compile an automaton from a regular expression\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... [FILES]...\n", "gfsmcompre"); + + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -lLABFILE --labels=LABFILE Basic label alphabet\n"); + printf(" -sREGEX --regex=REGEX Regular expression to compile\n"); + printf(" -x --string Compile as string, not regex\n"); + printf(" -rSRTYPE --semiring=SRTYPE Specify semiring type\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->labels_arg = NULL; + args_info->regex_arg = NULL; + args_info->string_flag = 0; + args_info->semiring_arg = strdup("tropical"); + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->labels_given = 0; + args_info->regex_given = 0; + args_info->string_given = 0; + args_info->semiring_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "labels", 1, NULL, 'l' }, + { "regex", 1, NULL, 's' }, + { "string", 0, NULL, 'x' }, + { "semiring", 1, NULL, 'r' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'l', ':', + 's', ':', + 'x', + 'r', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'l': /* Basic label alphabet */ + if (args_info->labels_given) { + fprintf(stderr, "%s: `--labels' (`-l') option given more than once\n", PROGRAM); + } + args_info->labels_given++; + if (args_info->labels_arg) free(args_info->labels_arg); + args_info->labels_arg = strdup(val); + break; + + case 's': /* Regular expression to compile */ + if (args_info->regex_given) { + fprintf(stderr, "%s: `--regex' (`-s') option given more than once\n", PROGRAM); + } + args_info->regex_given++; + if (args_info->regex_arg) free(args_info->regex_arg); + args_info->regex_arg = strdup(val); + break; + + case 'x': /* Compile as string, not regex */ + if (args_info->string_given) { + fprintf(stderr, "%s: `--string' (`-x') option given more than once\n", PROGRAM); + } + args_info->string_given++; + if (args_info->string_given <= 1) + args_info->string_flag = !(args_info->string_flag); + break; + + case 'r': /* Specify semiring type */ + if (args_info->semiring_given) { + fprintf(stderr, "%s: `--semiring' (`-r') option given more than once\n", PROGRAM); + } + args_info->semiring_given++; + if (args_info->semiring_arg) free(args_info->semiring_arg); + args_info->semiring_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Basic label alphabet */ + else if (strcmp(olong, "labels") == 0) { + if (args_info->labels_given) { + fprintf(stderr, "%s: `--labels' (`-l') option given more than once\n", PROGRAM); + } + args_info->labels_given++; + if (args_info->labels_arg) free(args_info->labels_arg); + args_info->labels_arg = strdup(val); + } + + /* Regular expression to compile */ + else if (strcmp(olong, "regex") == 0) { + if (args_info->regex_given) { + fprintf(stderr, "%s: `--regex' (`-s') option given more than once\n", PROGRAM); + } + args_info->regex_given++; + if (args_info->regex_arg) free(args_info->regex_arg); + args_info->regex_arg = strdup(val); + } + + /* Compile as string, not regex */ + else if (strcmp(olong, "string") == 0) { + if (args_info->string_given) { + fprintf(stderr, "%s: `--string' (`-x') option given more than once\n", PROGRAM); + } + args_info->string_given++; + if (args_info->string_given <= 1) + args_info->string_flag = !(args_info->string_flag); + } + + /* Specify semiring type */ + else if (strcmp(olong, "semiring") == 0) { + if (args_info->semiring_given) { + fprintf(stderr, "%s: `--semiring' (`-r') option given more than once\n", PROGRAM); + } + args_info->semiring_given++; + if (args_info->semiring_arg) free(args_info->semiring_arg); + args_info->semiring_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmcompre_cmdparser.h b/gfsm/gfsm/src/programs/gfsmcompre_cmdparser.h new file mode 100644 index 0000000..32361ea --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompre_cmdparser.h @@ -0,0 +1,71 @@ +/* -*- Mode: C -*- + * + * File: gfsmcompre_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmcompre_cmdparser_h +#define gfsmcompre_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * labels_arg; /* Basic label alphabet (default=NULL). */ + char * regex_arg; /* Regular expression to compile (default=NULL). */ + int string_flag; /* Compile as string, not regex (default=0). */ + char * semiring_arg; /* Specify semiring type (default=tropical). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int labels_given; /* Whether labels was given */ + int regex_given; /* Whether regex was given */ + int string_given; /* Whether string was given */ + int semiring_given; /* Whether semiring was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmcompre_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmcompre_main.c b/gfsm/gfsm/src/programs/gfsmcompre_main.c new file mode 100644 index 0000000..78e7721 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmcompre_main.c @@ -0,0 +1,143 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +#include <gfsm.h> + +#include "gfsmcompre_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmcompre"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *fstfilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmRegexCompiler *rec = NULL; +gfsmAlphabet *abet = NULL; +gfsmError *err = NULL; +gboolean emit_warnings = TRUE; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- sanity checks + if (!args.regex_given) { + g_printerr("%s: no regex specified!\n", progname); + cmdline_parser_print_help(); + exit(-1); + } + + //-- filenames + outfilename = args.output_arg; + + //-- alphabet: basic labels + abet = gfsm_string_alphabet_new(); + if (args.labels_given) { + if (!gfsm_alphabet_load_filename(abet, args.labels_arg, &err)) { + g_printerr("%s: load failed for labels file '%s': %s\n", + progname, args.labels_arg, err->message); + exit(2); + } + } + + //-- options for regex compiler + rec = gfsm_regex_compiler_new_full("gfsmRegexCompiler", + abet, + gfsm_sr_name_to_type(args.semiring_arg), + emit_warnings); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmAutomaton *fsm; + char *regex=NULL; + + GFSM_INIT + get_my_options(argc,argv); + + //-- string-compile hack: escape everything + if (args.string_flag) { + GString *tmp = g_string_new(""); + int i; + for (i=0; i < strlen(args.regex_arg); i++) { + g_string_append_c(tmp,'\\'); + g_string_append_c(tmp,args.regex_arg[i]); + } + regex = tmp->str; + g_string_free(tmp,FALSE); + } else { + regex = args.regex_arg; + } + + //-- parse regex string + gfsm_scanner_scan_string(&(rec->scanner), regex); + fsm = gfsm_regex_compiler_parse(rec); + + //-- check for errors + if (rec->scanner.err) { + g_printerr("%s: %s\n", progname, err->message); + exit(3); + } + if (!fsm) { + g_printerr("%s: no automaton!\n", progname); + exit(4); + } + + + //-- save output fsm + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(5); + } + + //-- cleanup + gfsm_regex_compiler_free(rec,TRUE,TRUE); + + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmconcat.gog b/gfsm/gfsm/src/programs/gfsmconcat.gog new file mode 100644 index 0000000..f9b1e47 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconcat.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmconcat" +#program_version "0.01" + +purpose "Concatenate finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE(s)" "Stored binary gfsm file(s)" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmconcat_cmdparser.c b/gfsm/gfsm/src/programs/gfsmconcat_cmdparser.c new file mode 100644 index 0000000..617a630 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconcat_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmconcat_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmconcat_cmdparser gfsmconcat.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmconcat" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmconcat_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmconcat (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Concatenate finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE(s)\n", "gfsmconcat"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE(s) Stored binary gfsm file(s)\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmconcat_cmdparser.h b/gfsm/gfsm/src/programs/gfsmconcat_cmdparser.h new file mode 100644 index 0000000..43125dd --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconcat_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmconcat_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmconcat_cmdparser_h +#define gfsmconcat_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmconcat_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmconcat_main.c b/gfsm/gfsm/src/programs/gfsmconcat_main.c new file mode 100644 index 0000000..9d63792 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconcat_main.c @@ -0,0 +1,118 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmconcat_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmconcat"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmOut=NULL, *fsmIn=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- require at least one file argument + if (args.inputs_num < 1) { + cmdline_parser_print_help(); + exit(2); + } + + //-- output + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsmIn = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * compute_operation() + * + utility routine + */ +void compute_operation(const char *infilename) +{ + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsmIn,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute concat + if (fsmOut == NULL) { + fsmOut = fsmIn; + fsmIn = gfsm_automaton_new(); + } else { + gfsm_automaton_concat(fsmOut,fsmIn); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + get_my_options(argc,argv); + + for (i = 0; i < args.inputs_num; i++) { + compute_operation(args.inputs[i]); + } + if (args.inputs_num == 1) compute_operation("-"); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmOut,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsmIn) gfsm_automaton_free(fsmIn); + if (fsmOut) gfsm_automaton_free(fsmOut); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmconnect.gog b/gfsm/gfsm/src/programs/gfsmconnect.gog new file mode 100644 index 0000000..2de8230 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconnect.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmconnect" +#program_version "0.01" + +purpose "Remove non-coaccessible states from finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmconnect_cmdparser.c b/gfsm/gfsm/src/programs/gfsmconnect_cmdparser.c new file mode 100644 index 0000000..a3f30cb --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconnect_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmconnect_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmconnect_cmdparser gfsmconnect.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmconnect" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmconnect_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmconnect (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Remove non-coaccessible states from finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmconnect"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmconnect_cmdparser.h b/gfsm/gfsm/src/programs/gfsmconnect_cmdparser.h new file mode 100644 index 0000000..08f42e0 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconnect_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmconnect_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmconnect_cmdparser_h +#define gfsmconnect_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmconnect_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmconnect_main.c b/gfsm/gfsm/src/programs/gfsmconnect_main.c new file mode 100644 index 0000000..82c9c22 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconnect_main.c @@ -0,0 +1,99 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmconnect_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmconnect"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + int rc = 0; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- connect + gfsm_automaton_connect(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- get exit status: 255 if no root state + if (fsm->root_id == gfsmNoState) rc=255; + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return rc; +} diff --git a/gfsm/gfsm/src/programs/gfsmconvert.gog b/gfsm/gfsm/src/programs/gfsmconvert.gog new file mode 100644 index 0000000..4bb24af --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconvert.gog @@ -0,0 +1,89 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmconvert" +#program_version "0.01" + +purpose "Convert binary format gfsm files" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "FSMFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "transducer" t "Specify whether automaton is a transducer" \ + arg="BOOL" + +int "weighted" w "Specify whether automaton is weighted" \ + arg="BOOL" + +string "semiring" s "Specify semiring type." \ + arg="SRTYPE" \ + details=" +Specify one of the following: + + boolean + log + real + trivial + tropical + +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmconvert_cmdparser.c b/gfsm/gfsm/src/programs/gfsmconvert_cmdparser.c new file mode 100644 index 0000000..3e77ce5 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconvert_cmdparser.c @@ -0,0 +1,517 @@ +/* -*- Mode: C -*- + * + * File: gfsmconvert_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmconvert_cmdparser gfsmconvert.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmconvert" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmconvert_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmconvert (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Convert binary format gfsm files\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... FSMFILE\n", "gfsmconvert"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" FSMFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -tBOOL --transducer=BOOL Specify whether automaton is a transducer\n"); + printf(" -wBOOL --weighted=BOOL Specify whether automaton is weighted\n"); + printf(" -sSRTYPE --semiring=SRTYPE Specify semiring type.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->transducer_arg = 0; + args_info->weighted_arg = 0; + args_info->semiring_arg = NULL; + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->transducer_given = 0; + args_info->weighted_given = 0; + args_info->semiring_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "transducer", 1, NULL, 't' }, + { "weighted", 1, NULL, 'w' }, + { "semiring", 1, NULL, 's' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 't', ':', + 'w', ':', + 's', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 't': /* Specify whether automaton is a transducer */ + if (args_info->transducer_given) { + fprintf(stderr, "%s: `--transducer' (`-t') option given more than once\n", PROGRAM); + } + args_info->transducer_given++; + args_info->transducer_arg = (int)atoi(val); + break; + + case 'w': /* Specify whether automaton is weighted */ + if (args_info->weighted_given) { + fprintf(stderr, "%s: `--weighted' (`-w') option given more than once\n", PROGRAM); + } + args_info->weighted_given++; + args_info->weighted_arg = (int)atoi(val); + break; + + case 's': /* Specify semiring type. */ + if (args_info->semiring_given) { + fprintf(stderr, "%s: `--semiring' (`-s') option given more than once\n", PROGRAM); + } + args_info->semiring_given++; + if (args_info->semiring_arg) free(args_info->semiring_arg); + args_info->semiring_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify whether automaton is a transducer */ + else if (strcmp(olong, "transducer") == 0) { + if (args_info->transducer_given) { + fprintf(stderr, "%s: `--transducer' (`-t') option given more than once\n", PROGRAM); + } + args_info->transducer_given++; + args_info->transducer_arg = (int)atoi(val); + } + + /* Specify whether automaton is weighted */ + else if (strcmp(olong, "weighted") == 0) { + if (args_info->weighted_given) { + fprintf(stderr, "%s: `--weighted' (`-w') option given more than once\n", PROGRAM); + } + args_info->weighted_given++; + args_info->weighted_arg = (int)atoi(val); + } + + /* Specify semiring type. */ + else if (strcmp(olong, "semiring") == 0) { + if (args_info->semiring_given) { + fprintf(stderr, "%s: `--semiring' (`-s') option given more than once\n", PROGRAM); + } + args_info->semiring_given++; + if (args_info->semiring_arg) free(args_info->semiring_arg); + args_info->semiring_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmconvert_cmdparser.h b/gfsm/gfsm/src/programs/gfsmconvert_cmdparser.h new file mode 100644 index 0000000..93a2291 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconvert_cmdparser.h @@ -0,0 +1,69 @@ +/* -*- Mode: C -*- + * + * File: gfsmconvert_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmconvert_cmdparser_h +#define gfsmconvert_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int transducer_arg; /* Specify whether automaton is a transducer (default=0). */ + int weighted_arg; /* Specify whether automaton is weighted (default=0). */ + char * semiring_arg; /* Specify semiring type. (default=NULL). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int transducer_given; /* Whether transducer was given */ + int weighted_given; /* Whether weighted was given */ + int semiring_given; /* Whether semiring was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmconvert_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmconvert_main.c b/gfsm/gfsm/src/programs/gfsmconvert_main.c new file mode 100644 index 0000000..7c09f08 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmconvert_main.c @@ -0,0 +1,110 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <gfsm.h> + +#include "gfsmconvert_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmconvert"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; +gfsmError *err = NULL; +gfsmSRType srtype = gfsmSRTUnknown; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + outfilename = args.output_arg; + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(3); + } + + //-- set flags + if (args.transducer_given) fsm->flags.is_transducer = args.transducer_arg; + if (args.weighted_given) fsm->flags.is_weighted = args.weighted_arg; + + //-- set semiring + if (args.semiring_given) { + srtype = gfsm_sr_name_to_type(args.semiring_arg); + if (srtype == gfsmSRTUnknown) { + g_printerr("%s: Warning: unknown semiring name '%s' defaults to type 'tropical'.\n", + progname, args.semiring_arg); + srtype = gfsmSRTTropical; + } + if (srtype != fsm->sr->type) { + gfsm_automaton_set_semiring(fsm, gfsm_semiring_new(srtype)); + } + } + + //-- store automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + gfsm_automaton_free(fsm); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmdeterminize.gog b/gfsm/gfsm/src/programs/gfsmdeterminize.gog new file mode 100644 index 0000000..1fed7e8 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdeterminize.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmdeterminize" +#program_version "0.01" + +purpose "Determinize a finite state machine" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmdeterminize_cmdparser.c b/gfsm/gfsm/src/programs/gfsmdeterminize_cmdparser.c new file mode 100644 index 0000000..0ce40e9 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdeterminize_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmdeterminize_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmdeterminize_cmdparser gfsmdeterminize.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmdeterminize" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmdeterminize_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmdeterminize (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Determinize a finite state machine\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmdeterminize"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmdeterminize_cmdparser.h b/gfsm/gfsm/src/programs/gfsmdeterminize_cmdparser.h new file mode 100644 index 0000000..a0324f4 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdeterminize_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmdeterminize_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmdeterminize_cmdparser_h +#define gfsmdeterminize_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmdeterminize_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmdeterminize_main.c b/gfsm/gfsm/src/programs/gfsmdeterminize_main.c new file mode 100644 index 0000000..66ba402 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdeterminize_main.c @@ -0,0 +1,100 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmdeterminize_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmdeterminize"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + + GFSM_INIT + + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- determinize + gfsm_automaton_determinize(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm, outfilename, args.compress_arg, &err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmdifference.gog b/gfsm/gfsm/src/programs/gfsmdifference.gog new file mode 100644 index 0000000..9bdb612 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdifference.gog @@ -0,0 +1,71 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmdifference" +#program_version "0.01" + +purpose "Compute difference of epsilon-free finite state acceptors" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE(s)" "Stored binary gfsm file(s)" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +Epsilon arcs are not handled correctly. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmdifference_cmdparser.c b/gfsm/gfsm/src/programs/gfsmdifference_cmdparser.c new file mode 100644 index 0000000..80c51f7 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdifference_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmdifference_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmdifference_cmdparser gfsmdifference.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmdifference" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmdifference_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmdifference (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute difference of epsilon-free finite state acceptors\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE(s)\n", "gfsmdifference"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE(s) Stored binary gfsm file(s)\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmdifference_cmdparser.h b/gfsm/gfsm/src/programs/gfsmdifference_cmdparser.h new file mode 100644 index 0000000..b61aa90 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdifference_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmdifference_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmdifference_cmdparser_h +#define gfsmdifference_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmdifference_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmdifference_main.c b/gfsm/gfsm/src/programs/gfsmdifference_main.c new file mode 100644 index 0000000..19aaedf --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdifference_main.c @@ -0,0 +1,123 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmdifference_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmdifference"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmOut=NULL, *fsmIn=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- require at least one file argument + if (args.inputs_num < 1) { + cmdline_parser_print_help(); + exit(2); + } + + //-- output + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsmIn = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * compute_operation() + * + utility routine + */ +void compute_operation(const char *infilename) +{ + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsmIn,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute difference + if (fsmOut == NULL) { + fsmOut = fsmIn; + fsmIn = gfsm_automaton_new(); + } else { + gfsm_automaton_difference(fsmOut,fsmIn); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + + GFSM_INIT + + get_my_options(argc,argv); + + for (i = 0; i < args.inputs_num; i++) { + compute_operation(args.inputs[i]); + } + if (args.inputs_num == 1) compute_operation("-"); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmOut,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsmIn) gfsm_automaton_free(fsmIn); + if (fsmOut) gfsm_automaton_free(fsmOut); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmdraw.gog b/gfsm/gfsm/src/programs/gfsmdraw.gog new file mode 100644 index 0000000..63fbb4f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdraw.gog @@ -0,0 +1,138 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmdraw" +#program_version "0.01" + +purpose "Generate graph specifications from binary gfsm files" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details " +gfsmdraw reads a binary GFSM file file and produces a graph specification +in either dot(1) or vcg(1) format. +" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Common Options +#----------------------------------------------------------------------------- +group "Common Options" + +string "ilabels" i "Specify input (lower) labels file." \ + arg="LABELS" + +string "olabels" o "Specify output (upper) labels file." \ + arg="LABELS" + +string "slabels" s "Specify state labels file." \ + arg="LABELS" + +string "title" t "Specify output title." \ + arg="TITLE" \ + details="Default is input filename." + +flag "vertical" v "Draw vertically." \ + details="Default mode is top-to-bottom" + +string "output" F "Output graph specification." \ + arg="DOTFILE" \ + details=" +Default behavior is to write to stdout. +" + +#----------------------------------------------------------------------------- +# dot Options +#----------------------------------------------------------------------------- +group "dot Options" + +flag "dot" d "Request dot output (default)" + +float "width" W "Specify output width (default=bbox)." \ + arg="WIDTH" \ + default="8.5" +float "height" H "Specify output height (default=bbox)." \ + arg="HEIGHT" \ + default="11" + +int "fontsize" f "Set output font size." \ + arg="POINTS" \ + default="14" +string "font" - "Set output font name." \ + arg="FONT" + +float "nodesep" n "Set min node separation." \ + default="0.25" +float "ranksep" r "Set min rank separation." \ + default="0.40" + +flag "portrait" p "Generate in portrait mode." \ + details="Default mode is landscape." + +#----------------------------------------------------------------------------- +# VCG Options +#----------------------------------------------------------------------------- +group "VCG Options" + +flag "vcg" g "Request VCG output." + +int "xspace" x "Set xspace graph parameter." \ + default="40" +int "yspace" y "Set yspace graph parameter." \ + default="20" + +string "state-shape" - "Set shape for state nodes." \ + arg="SHAPE" \ + default="box" + +string "state-color" - "Set default state color." \ + arg="COLOR" \ + default="white" + +string "final-color" - "Set color for final states." \ + arg="COLOR" \ + default="lightgrey" + + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils>, +dot(1), +vcg(1), +xvcg(1) +" diff --git a/gfsm/gfsm/src/programs/gfsmdraw_cmdparser.c b/gfsm/gfsm/src/programs/gfsmdraw_cmdparser.c new file mode 100644 index 0000000..40b52e6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdraw_cmdparser.c @@ -0,0 +1,835 @@ +/* -*- Mode: C -*- + * + * File: gfsmdraw_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmdraw_cmdparser gfsmdraw.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmdraw" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmdraw_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmdraw (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Generate graph specifications from binary gfsm files\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmdraw"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf("\n"); + printf(" Common Options:\n"); + printf(" -iLABELS --ilabels=LABELS Specify input (lower) labels file.\n"); + printf(" -oLABELS --olabels=LABELS Specify output (upper) labels file.\n"); + printf(" -sLABELS --slabels=LABELS Specify state labels file.\n"); + printf(" -tTITLE --title=TITLE Specify output title.\n"); + printf(" -v --vertical Draw vertically.\n"); + printf(" -FDOTFILE --output=DOTFILE Output graph specification.\n"); + printf("\n"); + printf(" dot Options:\n"); + printf(" -d --dot Request dot output (default)\n"); + printf(" -WFLOAT --width=FLOAT Specify output width (default=bbox).\n"); + printf(" -HFLOAT --height=FLOAT Specify output height (default=bbox).\n"); + printf(" -fPOINTS --fontsize=POINTS Set output font size.\n"); + printf(" --font=FONT Set output font name.\n"); + printf(" -nFLOAT --nodesep=FLOAT Set min node separation.\n"); + printf(" -rFLOAT --ranksep=FLOAT Set min rank separation.\n"); + printf(" -p --portrait Generate in portrait mode.\n"); + printf("\n"); + printf(" VCG Options:\n"); + printf(" -g --vcg Request VCG output.\n"); + printf(" -xINT --xspace=INT Set xspace graph parameter.\n"); + printf(" -yINT --yspace=INT Set yspace graph parameter.\n"); + printf(" --state-shape=SHAPE Set shape for state nodes.\n"); + printf(" --state-color=COLOR Set default state color.\n"); + printf(" --final-color=COLOR Set color for final states.\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->ilabels_arg = NULL; + args_info->olabels_arg = NULL; + args_info->slabels_arg = NULL; + args_info->title_arg = NULL; + args_info->vertical_flag = 0; + args_info->output_arg = NULL; + args_info->dot_flag = 0; + args_info->width_arg = 8.5; + args_info->height_arg = 11; + args_info->fontsize_arg = 14; + args_info->font_arg = NULL; + args_info->nodesep_arg = 0.25; + args_info->ranksep_arg = 0.40; + args_info->portrait_flag = 0; + args_info->vcg_flag = 0; + args_info->xspace_arg = 40; + args_info->yspace_arg = 20; + args_info->state_shape_arg = strdup("box"); + args_info->state_color_arg = strdup("white"); + args_info->final_color_arg = strdup("lightgrey"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->ilabels_given = 0; + args_info->olabels_given = 0; + args_info->slabels_given = 0; + args_info->title_given = 0; + args_info->vertical_given = 0; + args_info->output_given = 0; + args_info->dot_given = 0; + args_info->width_given = 0; + args_info->height_given = 0; + args_info->fontsize_given = 0; + args_info->font_given = 0; + args_info->nodesep_given = 0; + args_info->ranksep_given = 0; + args_info->portrait_given = 0; + args_info->vcg_given = 0; + args_info->xspace_given = 0; + args_info->yspace_given = 0; + args_info->state_shape_given = 0; + args_info->state_color_given = 0; + args_info->final_color_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "ilabels", 1, NULL, 'i' }, + { "olabels", 1, NULL, 'o' }, + { "slabels", 1, NULL, 's' }, + { "title", 1, NULL, 't' }, + { "vertical", 0, NULL, 'v' }, + { "output", 1, NULL, 'F' }, + { "dot", 0, NULL, 'd' }, + { "width", 1, NULL, 'W' }, + { "height", 1, NULL, 'H' }, + { "fontsize", 1, NULL, 'f' }, + { "font", 1, NULL, 0 }, + { "nodesep", 1, NULL, 'n' }, + { "ranksep", 1, NULL, 'r' }, + { "portrait", 0, NULL, 'p' }, + { "vcg", 0, NULL, 'g' }, + { "xspace", 1, NULL, 'x' }, + { "yspace", 1, NULL, 'y' }, + { "state-shape", 1, NULL, 0 }, + { "state-color", 1, NULL, 0 }, + { "final-color", 1, NULL, 0 }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'i', ':', + 'o', ':', + 's', ':', + 't', ':', + 'v', + 'F', ':', + 'd', + 'W', ':', + 'H', ':', + 'f', ':', + 'n', ':', + 'r', ':', + 'p', + 'g', + 'x', ':', + 'y', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'i': /* Specify input (lower) labels file. */ + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + break; + + case 'o': /* Specify output (upper) labels file. */ + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + break; + + case 's': /* Specify state labels file. */ + if (args_info->slabels_given) { + fprintf(stderr, "%s: `--slabels' (`-s') option given more than once\n", PROGRAM); + } + args_info->slabels_given++; + if (args_info->slabels_arg) free(args_info->slabels_arg); + args_info->slabels_arg = strdup(val); + break; + + case 't': /* Specify output title. */ + if (args_info->title_given) { + fprintf(stderr, "%s: `--title' (`-t') option given more than once\n", PROGRAM); + } + args_info->title_given++; + if (args_info->title_arg) free(args_info->title_arg); + args_info->title_arg = strdup(val); + break; + + case 'v': /* Draw vertically. */ + if (args_info->vertical_given) { + fprintf(stderr, "%s: `--vertical' (`-v') option given more than once\n", PROGRAM); + } + args_info->vertical_given++; + if (args_info->vertical_given <= 1) + args_info->vertical_flag = !(args_info->vertical_flag); + break; + + case 'F': /* Output graph specification. */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 'd': /* Request dot output (default) */ + if (args_info->dot_given) { + fprintf(stderr, "%s: `--dot' (`-d') option given more than once\n", PROGRAM); + } + args_info->dot_given++; + if (args_info->dot_given <= 1) + args_info->dot_flag = !(args_info->dot_flag); + break; + + case 'W': /* Specify output width (default=bbox). */ + if (args_info->width_given) { + fprintf(stderr, "%s: `--width' (`-W') option given more than once\n", PROGRAM); + } + args_info->width_given++; + args_info->width_arg = (float)strtod(val, NULL); + break; + + case 'H': /* Specify output height (default=bbox). */ + if (args_info->height_given) { + fprintf(stderr, "%s: `--height' (`-H') option given more than once\n", PROGRAM); + } + args_info->height_given++; + args_info->height_arg = (float)strtod(val, NULL); + break; + + case 'f': /* Set output font size. */ + if (args_info->fontsize_given) { + fprintf(stderr, "%s: `--fontsize' (`-f') option given more than once\n", PROGRAM); + } + args_info->fontsize_given++; + args_info->fontsize_arg = (int)atoi(val); + break; + + case 'n': /* Set min node separation. */ + if (args_info->nodesep_given) { + fprintf(stderr, "%s: `--nodesep' (`-n') option given more than once\n", PROGRAM); + } + args_info->nodesep_given++; + args_info->nodesep_arg = (float)strtod(val, NULL); + break; + + case 'r': /* Set min rank separation. */ + if (args_info->ranksep_given) { + fprintf(stderr, "%s: `--ranksep' (`-r') option given more than once\n", PROGRAM); + } + args_info->ranksep_given++; + args_info->ranksep_arg = (float)strtod(val, NULL); + break; + + case 'p': /* Generate in portrait mode. */ + if (args_info->portrait_given) { + fprintf(stderr, "%s: `--portrait' (`-p') option given more than once\n", PROGRAM); + } + args_info->portrait_given++; + if (args_info->portrait_given <= 1) + args_info->portrait_flag = !(args_info->portrait_flag); + break; + + case 'g': /* Request VCG output. */ + if (args_info->vcg_given) { + fprintf(stderr, "%s: `--vcg' (`-g') option given more than once\n", PROGRAM); + } + args_info->vcg_given++; + if (args_info->vcg_given <= 1) + args_info->vcg_flag = !(args_info->vcg_flag); + break; + + case 'x': /* Set xspace graph parameter. */ + if (args_info->xspace_given) { + fprintf(stderr, "%s: `--xspace' (`-x') option given more than once\n", PROGRAM); + } + args_info->xspace_given++; + args_info->xspace_arg = (int)atoi(val); + break; + + case 'y': /* Set yspace graph parameter. */ + if (args_info->yspace_given) { + fprintf(stderr, "%s: `--yspace' (`-y') option given more than once\n", PROGRAM); + } + args_info->yspace_given++; + args_info->yspace_arg = (int)atoi(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify input (lower) labels file. */ + else if (strcmp(olong, "ilabels") == 0) { + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + } + + /* Specify output (upper) labels file. */ + else if (strcmp(olong, "olabels") == 0) { + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + } + + /* Specify state labels file. */ + else if (strcmp(olong, "slabels") == 0) { + if (args_info->slabels_given) { + fprintf(stderr, "%s: `--slabels' (`-s') option given more than once\n", PROGRAM); + } + args_info->slabels_given++; + if (args_info->slabels_arg) free(args_info->slabels_arg); + args_info->slabels_arg = strdup(val); + } + + /* Specify output title. */ + else if (strcmp(olong, "title") == 0) { + if (args_info->title_given) { + fprintf(stderr, "%s: `--title' (`-t') option given more than once\n", PROGRAM); + } + args_info->title_given++; + if (args_info->title_arg) free(args_info->title_arg); + args_info->title_arg = strdup(val); + } + + /* Draw vertically. */ + else if (strcmp(olong, "vertical") == 0) { + if (args_info->vertical_given) { + fprintf(stderr, "%s: `--vertical' (`-v') option given more than once\n", PROGRAM); + } + args_info->vertical_given++; + if (args_info->vertical_given <= 1) + args_info->vertical_flag = !(args_info->vertical_flag); + } + + /* Output graph specification. */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + /* Request dot output (default) */ + else if (strcmp(olong, "dot") == 0) { + if (args_info->dot_given) { + fprintf(stderr, "%s: `--dot' (`-d') option given more than once\n", PROGRAM); + } + args_info->dot_given++; + if (args_info->dot_given <= 1) + args_info->dot_flag = !(args_info->dot_flag); + } + + /* Specify output width (default=bbox). */ + else if (strcmp(olong, "width") == 0) { + if (args_info->width_given) { + fprintf(stderr, "%s: `--width' (`-W') option given more than once\n", PROGRAM); + } + args_info->width_given++; + args_info->width_arg = (float)strtod(val, NULL); + } + + /* Specify output height (default=bbox). */ + else if (strcmp(olong, "height") == 0) { + if (args_info->height_given) { + fprintf(stderr, "%s: `--height' (`-H') option given more than once\n", PROGRAM); + } + args_info->height_given++; + args_info->height_arg = (float)strtod(val, NULL); + } + + /* Set output font size. */ + else if (strcmp(olong, "fontsize") == 0) { + if (args_info->fontsize_given) { + fprintf(stderr, "%s: `--fontsize' (`-f') option given more than once\n", PROGRAM); + } + args_info->fontsize_given++; + args_info->fontsize_arg = (int)atoi(val); + } + + /* Set output font name. */ + else if (strcmp(olong, "font") == 0) { + if (args_info->font_given) { + fprintf(stderr, "%s: `--font' option given more than once\n", PROGRAM); + } + args_info->font_given++; + if (args_info->font_arg) free(args_info->font_arg); + args_info->font_arg = strdup(val); + } + + /* Set min node separation. */ + else if (strcmp(olong, "nodesep") == 0) { + if (args_info->nodesep_given) { + fprintf(stderr, "%s: `--nodesep' (`-n') option given more than once\n", PROGRAM); + } + args_info->nodesep_given++; + args_info->nodesep_arg = (float)strtod(val, NULL); + } + + /* Set min rank separation. */ + else if (strcmp(olong, "ranksep") == 0) { + if (args_info->ranksep_given) { + fprintf(stderr, "%s: `--ranksep' (`-r') option given more than once\n", PROGRAM); + } + args_info->ranksep_given++; + args_info->ranksep_arg = (float)strtod(val, NULL); + } + + /* Generate in portrait mode. */ + else if (strcmp(olong, "portrait") == 0) { + if (args_info->portrait_given) { + fprintf(stderr, "%s: `--portrait' (`-p') option given more than once\n", PROGRAM); + } + args_info->portrait_given++; + if (args_info->portrait_given <= 1) + args_info->portrait_flag = !(args_info->portrait_flag); + } + + /* Request VCG output. */ + else if (strcmp(olong, "vcg") == 0) { + if (args_info->vcg_given) { + fprintf(stderr, "%s: `--vcg' (`-g') option given more than once\n", PROGRAM); + } + args_info->vcg_given++; + if (args_info->vcg_given <= 1) + args_info->vcg_flag = !(args_info->vcg_flag); + } + + /* Set xspace graph parameter. */ + else if (strcmp(olong, "xspace") == 0) { + if (args_info->xspace_given) { + fprintf(stderr, "%s: `--xspace' (`-x') option given more than once\n", PROGRAM); + } + args_info->xspace_given++; + args_info->xspace_arg = (int)atoi(val); + } + + /* Set yspace graph parameter. */ + else if (strcmp(olong, "yspace") == 0) { + if (args_info->yspace_given) { + fprintf(stderr, "%s: `--yspace' (`-y') option given more than once\n", PROGRAM); + } + args_info->yspace_given++; + args_info->yspace_arg = (int)atoi(val); + } + + /* Set shape for state nodes. */ + else if (strcmp(olong, "state-shape") == 0) { + if (args_info->state_shape_given) { + fprintf(stderr, "%s: `--state-shape' option given more than once\n", PROGRAM); + } + args_info->state_shape_given++; + if (args_info->state_shape_arg) free(args_info->state_shape_arg); + args_info->state_shape_arg = strdup(val); + } + + /* Set default state color. */ + else if (strcmp(olong, "state-color") == 0) { + if (args_info->state_color_given) { + fprintf(stderr, "%s: `--state-color' option given more than once\n", PROGRAM); + } + args_info->state_color_given++; + if (args_info->state_color_arg) free(args_info->state_color_arg); + args_info->state_color_arg = strdup(val); + } + + /* Set color for final states. */ + else if (strcmp(olong, "final-color") == 0) { + if (args_info->final_color_given) { + fprintf(stderr, "%s: `--final-color' option given more than once\n", PROGRAM); + } + args_info->final_color_given++; + if (args_info->final_color_arg) free(args_info->final_color_arg); + args_info->final_color_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmdraw_cmdparser.h b/gfsm/gfsm/src/programs/gfsmdraw_cmdparser.h new file mode 100644 index 0000000..b494e48 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdraw_cmdparser.h @@ -0,0 +1,99 @@ +/* -*- Mode: C -*- + * + * File: gfsmdraw_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmdraw_cmdparser_h +#define gfsmdraw_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * ilabels_arg; /* Specify input (lower) labels file. (default=NULL). */ + char * olabels_arg; /* Specify output (upper) labels file. (default=NULL). */ + char * slabels_arg; /* Specify state labels file. (default=NULL). */ + char * title_arg; /* Specify output title. (default=NULL). */ + int vertical_flag; /* Draw vertically. (default=0). */ + char * output_arg; /* Output graph specification. (default=NULL). */ + int dot_flag; /* Request dot output (default) (default=0). */ + float width_arg; /* Specify output width (default=bbox). (default=8.5). */ + float height_arg; /* Specify output height (default=bbox). (default=11). */ + int fontsize_arg; /* Set output font size. (default=14). */ + char * font_arg; /* Set output font name. (default=NULL). */ + float nodesep_arg; /* Set min node separation. (default=0.25). */ + float ranksep_arg; /* Set min rank separation. (default=0.40). */ + int portrait_flag; /* Generate in portrait mode. (default=0). */ + int vcg_flag; /* Request VCG output. (default=0). */ + int xspace_arg; /* Set xspace graph parameter. (default=40). */ + int yspace_arg; /* Set yspace graph parameter. (default=20). */ + char * state_shape_arg; /* Set shape for state nodes. (default=box). */ + char * state_color_arg; /* Set default state color. (default=white). */ + char * final_color_arg; /* Set color for final states. (default=lightgrey). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int ilabels_given; /* Whether ilabels was given */ + int olabels_given; /* Whether olabels was given */ + int slabels_given; /* Whether slabels was given */ + int title_given; /* Whether title was given */ + int vertical_given; /* Whether vertical was given */ + int output_given; /* Whether output was given */ + int dot_given; /* Whether dot was given */ + int width_given; /* Whether width was given */ + int height_given; /* Whether height was given */ + int fontsize_given; /* Whether fontsize was given */ + int font_given; /* Whether font was given */ + int nodesep_given; /* Whether nodesep was given */ + int ranksep_given; /* Whether ranksep was given */ + int portrait_given; /* Whether portrait was given */ + int vcg_given; /* Whether vcg was given */ + int xspace_given; /* Whether xspace was given */ + int yspace_given; /* Whether yspace was given */ + int state_shape_given; /* Whether state-shape was given */ + int state_color_given; /* Whether state-color was given */ + int final_color_given; /* Whether final-color was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmdraw_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmdraw_main.c b/gfsm/gfsm/src/programs/gfsmdraw_main.c new file mode 100644 index 0000000..7748419 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmdraw_main.c @@ -0,0 +1,166 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmdraw_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmdraw"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; +gfsmAlphabet *ilabels=NULL, *olabels=NULL, *slabels=NULL; +gfsmError *err = NULL; + +typedef enum _gfsmDrawMode { + gfsmDMNone, + gfsmDMDot, + gfsmDMVCG +} gfsmDrawMode; +gfsmDrawMode mode = gfsmDMDot; //-- default mode + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + if (args.output_given) outfilename = args.output_arg; + + //-- labels: input + if (args.ilabels_given) { + ilabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(ilabels,args.ilabels_arg,&err)) { + g_printerr("%s: load failed for input-labels file '%s': %s\n", + progname, args.ilabels_arg, (err ? err->message : "?")); + exit(2); + } + } + //-- labels: output + if (args.olabels_given) { + olabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(olabels,args.olabels_arg,&err)) { + g_printerr("%s: load failed for output-labels file '%s': %s\n", + progname, args.olabels_arg, (err ? err->message : "?")); + exit(2); + } + } + //-- labels: state + if (args.slabels_given) { + slabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(slabels,args.slabels_arg,&err)) { + g_printerr("%s: load failed for state-labels file '%s': %s\n", + progname, args.slabels_arg, (err ? err->message : "?")); + exit(2); + } + } + + //-- draw mode + if (args.dot_given) mode = gfsmDMDot; + else if (args.vcg_given) mode = gfsmDMVCG; + + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gboolean rc = FALSE; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, (err ? err->message : "?")); + exit(3); + } + + //-- draw automaton + if (mode==gfsmDMDot) + rc = gfsm_automaton_draw_dot_filename_full(fsm, + outfilename, + ilabels, + olabels, + slabels, + (args.title_given ? args.title_arg : infilename), + args.width_arg, + args.height_arg, + args.fontsize_arg, + args.font_arg, + args.portrait_given, + args.vertical_given, + args.nodesep_arg, + args.ranksep_arg, + &err); + else if (mode==gfsmDMVCG) + rc = gfsm_automaton_draw_vcg_filename_full(fsm, + outfilename, + ilabels, + olabels, + slabels, + (args.title_given ? args.title_arg : infilename), + args.xspace_arg, + args.yspace_arg, + (args.vertical_given ? "top_to_bottom" : "left_to_right"), + args.state_shape_arg, + args.state_color_arg, + args.final_color_arg, + &err); + + if (!rc) { + g_printerr("%s: store failed to '%s': %s\n", + progname, outfilename, (err ? err->message : "?")); + exit(4); + } + + //-- cleanup + if (ilabels) gfsm_alphabet_free(ilabels); + if (olabels) gfsm_alphabet_free(olabels); + if (slabels) gfsm_alphabet_free(slabels); + gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmheader.gog b/gfsm/gfsm/src/programs/gfsmheader.gog new file mode 100644 index 0000000..c3555ee --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmheader.gog @@ -0,0 +1,57 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmheader" +#program_version "0.01" + +purpose "Show header information from stored gfsm files" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmheader_cmdparser.c b/gfsm/gfsm/src/programs/gfsmheader_cmdparser.c new file mode 100644 index 0000000..58f982f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmheader_cmdparser.c @@ -0,0 +1,403 @@ +/* -*- Mode: C -*- + * + * File: gfsmheader_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmheader_cmdparser gfsmheader.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmheader" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmheader_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmheader (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Show header information from stored gfsm files\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmheader"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmheader_cmdparser.h b/gfsm/gfsm/src/programs/gfsmheader_cmdparser.h new file mode 100644 index 0000000..b9e0baa --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmheader_cmdparser.h @@ -0,0 +1,60 @@ +/* -*- Mode: C -*- + * + * File: gfsmheader_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmheader_cmdparser_h +#define gfsmheader_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmheader_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmheader_main.c b/gfsm/gfsm/src/programs/gfsmheader_main.c new file mode 100644 index 0000000..5ba0a87 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmheader_main.c @@ -0,0 +1,129 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmheader_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmheader"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; + +//-- global structs +gfsmAutomatonHeader hdr; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + + //-- initialize header + memset(&hdr, 0, sizeof(gfsmAutomatonHeader)); +} + +/*-------------------------------------------------------------------------- + * Utilities + *--------------------------------------------------------------------------*/ +#define bool2char(b) (b ? 'y' : 'n') + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + gfsmIOHandle *ioh = NULL; + GString *modestr = NULL; + + GFSM_INIT + + get_my_options(argc,argv); + + //-- open file + if (!(ioh = gfsmio_new_filename(infilename,"rb",-1,&err)) || err) { + g_printerr("%s: open failed for '%s': %s\n", progname, infilename, err->message); + exit(2); + } + + //-- read header + if (!gfsmio_read(ioh, &hdr, sizeof(gfsmAutomatonHeader))) { + g_printerr("%s: failed to read header!\n", progname); + exit(3); + } + gfsmio_close(ioh); + + //-- print header information + printf("%-24s: %s\n", "Filename", infilename); + printf("%-24s: %s\n", "magic", hdr.magic); + printf("%-24s: %d.%d.%d\n", "version", + hdr.version.major, hdr.version.minor, hdr.version.micro); + printf("%-24s: %d.%d.%d\n", "version_min", + hdr.version_min.major, hdr.version_min.minor, hdr.version_min.micro); + + printf("%-24s: %d\n", "flags.is_transducer", hdr.flags.is_transducer); + printf("%-24s: %d\n", "flags.is_weighted", hdr.flags.is_transducer); +#if 0 + printf("%-24s: %d (%s)\n", "flags.sort_mode", + hdr.flags.sort_mode, gfsm_arc_sortmode_to_name(hdr.flags.sort_mode)); +#else + modestr = gfsm_acmask_to_gstring(hdr.flags.sort_mode, modestr); + printf("%-24s: %d (%s)\n", "flags.sort_mode", hdr.flags.sort_mode, modestr->str); + g_string_free(modestr,TRUE); +#endif + printf("%-24s: %d\n", "flags.is_deterministic", hdr.flags.is_deterministic); + printf("%-24s: %d\n", "flags.unused", hdr.flags.unused); + + printf("%-24s: %u\n", "root_id", hdr.root_id); + printf("%-24s: %u\n", "n_states", hdr.n_states); + printf("%-24s: %u\n", "n_arcs", hdr.n_arcs_007); + printf("%-24s: %u (%s)\n", "srtype", hdr.srtype, gfsm_sr_type_to_name(hdr.srtype)); + + printf("%-24s: %u\n", "unused1", hdr.unused1); + printf("%-24s: %u\n", "unused2", hdr.unused2); + printf("%-24s: %u\n", "unused3", hdr.unused3); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmindex.gog b/gfsm/gfsm/src/programs/gfsmindex.gog new file mode 100644 index 0000000..53c2a14 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmindex.gog @@ -0,0 +1,75 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmindex" +#program_version "0.01" + +purpose "Convert between indexed and un-indexed binary gfsm automata" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "FSMFILE" "Stored binary gfsm automaton file or index" \ + details=" +If unspecified, standard input will be read. +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +flag "unindex" u "Convert indexed automaton to unindexed format" \ + details=" +Default behavior is to convert a plain unindexed automaton to an indexed automaton. +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmindex_cmdparser.c b/gfsm/gfsm/src/programs/gfsmindex_cmdparser.c new file mode 100644 index 0000000..8de1602 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmindex_cmdparser.c @@ -0,0 +1,473 @@ +/* -*- Mode: C -*- + * + * File: gfsmindex_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.06 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmindex_cmdparser gfsmindex.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmindex" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmindex_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmindex (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Convert between indexed and un-indexed binary gfsm automata\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... FSMFILE\n", "gfsmindex"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" FSMFILE Stored binary gfsm automaton file or index\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -u --unindex Convert indexed automaton to unindexed format\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->unindex_flag = 0; + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->unindex_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "unindex", 0, NULL, 'u' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'u', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'u': /* Convert indexed automaton to unindexed format */ + if (args_info->unindex_given) { + fprintf(stderr, "%s: `--unindex' (`-u') option given more than once\n", PROGRAM); + } + args_info->unindex_given++; + if (args_info->unindex_given <= 1) + args_info->unindex_flag = !(args_info->unindex_flag); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Convert indexed automaton to unindexed format */ + else if (strcmp(olong, "unindex") == 0) { + if (args_info->unindex_given) { + fprintf(stderr, "%s: `--unindex' (`-u') option given more than once\n", PROGRAM); + } + args_info->unindex_given++; + if (args_info->unindex_given <= 1) + args_info->unindex_flag = !(args_info->unindex_flag); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmindex_cmdparser.h b/gfsm/gfsm/src/programs/gfsmindex_cmdparser.h new file mode 100644 index 0000000..7dbcfb6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmindex_cmdparser.h @@ -0,0 +1,65 @@ +/* -*- Mode: C -*- + * + * File: gfsmindex_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.06. + * + */ + +#ifndef gfsmindex_cmdparser_h +#define gfsmindex_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int unindex_flag; /* Convert indexed automaton to unindexed format (default=0). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int unindex_given; /* Whether unindex was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmindex_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmindex_main.c b/gfsm/gfsm/src/programs/gfsmindex_main.c new file mode 100644 index 0000000..76adf26 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmindex_main.c @@ -0,0 +1,124 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2007 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <gfsm.h> + +#include "gfsmindex_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmindex"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm=NULL; +gfsmIndexedAutomaton *xfsm=NULL; +gfsmError *err=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + outfilename = args.output_arg; +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + get_my_options(argc,argv); + + //-- dispatch + if (args.unindex_given) { + //-- convert indexed --> vanilla + + //-- load index + xfsm = gfsm_indexed_automaton_new(); + if (!gfsm_indexed_automaton_load_bin_filename(xfsm,infilename,&err)) { + g_printerr("%s: load failed for indexed automaton from '%s': %s\n", progname, infilename, + (err ? err->message : "?")); + exit(3); + } + + //-- unindex + fsm = gfsm_indexed_to_automaton(xfsm,NULL); + + //-- store vanilla + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed for vanilla automaton to '%s': %s\n", progname, outfilename, + (err ? err->message : "?")); + exit(4); + } + } + else { + //-- convert vanilla --> indexed + + //-- load vanilla + fsm = gfsm_automaton_new(); + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for vanilla automaton from '%s': %s\n", progname, infilename, + (err ? err->message : "?")); + exit(3); + } + + //-- index & sort + xfsm = gfsm_automaton_to_indexed(fsm,NULL); + //gfsm_indexed_automaton_sort(xfsm, gfsm_acmask_from_args(gfsmACLower,gfsmACWeight)); //-- TODO: make these options! + + //-- store indexed + if (!gfsm_indexed_automaton_save_bin_filename(xfsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed for indexed automaton to '%s': %s\n", progname, outfilename, + (err ? err->message : "?")); + exit(4); + } + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + if (xfsm) gfsm_indexed_automaton_free(xfsm); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsminfo.gog b/gfsm/gfsm/src/programs/gfsminfo.gog new file mode 100644 index 0000000..22ddbec --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminfo.gog @@ -0,0 +1,57 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsminfo" +#program_version "0.01" + +purpose "Show basic information about stored gfsm files" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsminfo_cmdparser.c b/gfsm/gfsm/src/programs/gfsminfo_cmdparser.c new file mode 100644 index 0000000..79e978f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminfo_cmdparser.c @@ -0,0 +1,403 @@ +/* -*- Mode: C -*- + * + * File: gfsminfo_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsminfo_cmdparser gfsminfo.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsminfo" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsminfo_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsminfo (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Show basic information about stored gfsm files\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsminfo"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsminfo_cmdparser.h b/gfsm/gfsm/src/programs/gfsminfo_cmdparser.h new file mode 100644 index 0000000..8aa6727 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminfo_cmdparser.h @@ -0,0 +1,60 @@ +/* -*- Mode: C -*- + * + * File: gfsminfo_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsminfo_cmdparser_h +#define gfsminfo_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsminfo_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsminfo_main.c b/gfsm/gfsm/src/programs/gfsminfo_main.c new file mode 100644 index 0000000..7c6377d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminfo_main.c @@ -0,0 +1,123 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsminfo_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsminfo"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * Utilities + *--------------------------------------------------------------------------*/ +#define bool2char(b) (b ? 'y' : 'n') + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + GString *modestr = NULL; + + GFSM_INIT + + get_my_options(argc,argv); + guint n_eps_i, n_eps_o, n_eps_io; + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(2); + } + + //-- print information + printf("%-24s: %s\n", "Filename", infilename); + printf("%-24s: %s\n", "Semiring", gfsm_sr_type_to_name(fsm->sr->type)); + printf("%-24s: %c\n", "Transducer?", bool2char(gfsm_automaton_is_transducer(fsm))); + printf("%-24s: %c\n", "Weighted?", bool2char(gfsm_automaton_is_weighted(fsm))); + printf("%-24s: %c\n", "Deterministic?", bool2char(fsm->flags.is_deterministic)); +#if 0 + printf("%-24s: %s\n", "Sort Mode", gfsm_arc_sortmode_to_name(gfsm_automaton_sortmode(fsm))); +#else + modestr = gfsm_acmask_to_gstring(fsm->flags.sort_mode, modestr); + printf("%-24s: %s\n", "Sort Mode", modestr->str); + g_string_free(modestr,TRUE); +#endif + if (fsm->root_id != gfsmNoState) { + printf("%-24s: %u\n", "Initial state", fsm->root_id); + } else { + printf("%-24s: %s\n", "Initial state", "none"); + } + printf("%-24s: %u\n", "# of states", gfsm_automaton_n_states(fsm)); + printf("%-24s: %u\n", "# of final states", gfsm_automaton_n_final_states(fsm)); + printf("%-24s: %u\n", "# of arcs", gfsm_automaton_n_arcs_full(fsm, &n_eps_i, &n_eps_o, &n_eps_io)); + printf("%-24s: %u\n", "# of i/o epsilon arcs", n_eps_io); + printf("%-24s: %u\n", "# of input epsilon arcs", n_eps_i); + printf("%-24s: %u\n", "# of output epsilon arcs", n_eps_o); + + printf("%-24s: %c\n", "cyclic?", bool2char(gfsm_automaton_is_cyclic(fsm))); + //... + + //-- cleanup + gfsm_automaton_free(fsm); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmintersect.gog b/gfsm/gfsm/src/programs/gfsmintersect.gog new file mode 100644 index 0000000..9ce5f79 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmintersect.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmintersect" +#program_version "0.01" + +purpose "Compute intersection of finite state acceptors" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE(s)" "Stored binary gfsm file(s)" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +Probably many. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmintersect_cmdparser.c b/gfsm/gfsm/src/programs/gfsmintersect_cmdparser.c new file mode 100644 index 0000000..28b385e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmintersect_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmintersect_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmintersect_cmdparser gfsmintersect.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmintersect" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmintersect_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmintersect (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute intersection of finite state acceptors\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE(s)\n", "gfsmintersect"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE(s) Stored binary gfsm file(s)\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmintersect_cmdparser.h b/gfsm/gfsm/src/programs/gfsmintersect_cmdparser.h new file mode 100644 index 0000000..4ff1144 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmintersect_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmintersect_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmintersect_cmdparser_h +#define gfsmintersect_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmintersect_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmintersect_main.c b/gfsm/gfsm/src/programs/gfsmintersect_main.c new file mode 100644 index 0000000..4fd05ff --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmintersect_main.c @@ -0,0 +1,123 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmintersect_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmintersect"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmOut=NULL, *fsmIn=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- require at least one file argument + if (args.inputs_num < 1) { + cmdline_parser_print_help(); + exit(2); + } + + //-- output + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsmIn = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * compute_operation() + * + utility routine + */ +void compute_operation(const char *infilename) +{ + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsmIn,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute underlying FSM operation + if (fsmOut == NULL) { + fsmOut = fsmIn; + fsmIn = gfsm_automaton_new(); + } else { + gfsm_automaton_intersect(fsmOut,fsmIn); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + + GFSM_INIT + + get_my_options(argc,argv); + + for (i = 0; i < args.inputs_num; i++) { + compute_operation(args.inputs[i]); + } + if (args.inputs_num == 1) compute_operation("-"); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmOut,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsmIn) gfsm_automaton_free(fsmIn); + if (fsmOut) gfsm_automaton_free(fsmOut); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsminvert.gog b/gfsm/gfsm/src/programs/gfsminvert.gog new file mode 100644 index 0000000..4bb0f2e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminvert.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsminvert" +#program_version "0.01" + +purpose "Invert input/output tapes of finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsminvert_cmdparser.c b/gfsm/gfsm/src/programs/gfsminvert_cmdparser.c new file mode 100644 index 0000000..04d03c1 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminvert_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsminvert_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsminvert_cmdparser gfsminvert.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsminvert" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsminvert_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsminvert (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Invert input/output tapes of finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsminvert"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsminvert_cmdparser.h b/gfsm/gfsm/src/programs/gfsminvert_cmdparser.h new file mode 100644 index 0000000..47b8bad --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminvert_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsminvert_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsminvert_cmdparser_h +#define gfsminvert_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsminvert_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsminvert_main.c b/gfsm/gfsm/src/programs/gfsminvert_main.c new file mode 100644 index 0000000..acb285c --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsminvert_main.c @@ -0,0 +1,95 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsminvert_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsminvert"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- invert + gfsm_automaton_invert(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmlabels.gog b/gfsm/gfsm/src/programs/gfsmlabels.gog new file mode 100644 index 0000000..56642da --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlabels.gog @@ -0,0 +1,78 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmlabels" +#program_version "0.01" + +purpose "Map input text characters to gfsm labels" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "TXTFILE" "Input text file" \ + details=" +If unspecified, standard input will be read. +" + +#argument "BINFILE" "Output binary gfsm file" \ +# details=" +#If unspecified, output will be written to standard output. +#" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "labels" l "Specify input alphabet (labels) file." \ + arg="LABELS" + +flag "att-mode" a "Parse string(s) in AT&T-compatible mode." \ + default="0" + +flag "map-mode" m "Output original strings in addition to label vectors." \ + default="0" + +flag "quiet" q "Suppress warnings about undefined symbols." \ + default="0" + +string "output" o "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmlabels_cmdparser.c b/gfsm/gfsm/src/programs/gfsmlabels_cmdparser.c new file mode 100644 index 0000000..078f4e5 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlabels_cmdparser.c @@ -0,0 +1,523 @@ +/* -*- Mode: C -*- + * + * File: gfsmlabels_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.06 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmlabels_cmdparser gfsmlabels.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmlabels" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmlabels_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmlabels (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Map input text characters to gfsm labels\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... TXTFILE\n", "gfsmlabels"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" TXTFILE Input text file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -lLABELS --labels=LABELS Specify input alphabet (labels) file.\n"); + printf(" -a --att-mode Parse string(s) in AT&T-compatible mode.\n"); + printf(" -m --map-mode Output original strings in addition to label vectors.\n"); + printf(" -q --quiet Suppress warnings about undefined symbols.\n"); + printf(" -oFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->labels_arg = NULL; + args_info->att_mode_flag = 0; + args_info->map_mode_flag = 0; + args_info->quiet_flag = 0; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->labels_given = 0; + args_info->att_mode_given = 0; + args_info->map_mode_given = 0; + args_info->quiet_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "labels", 1, NULL, 'l' }, + { "att-mode", 0, NULL, 'a' }, + { "map-mode", 0, NULL, 'm' }, + { "quiet", 0, NULL, 'q' }, + { "output", 1, NULL, 'o' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'l', ':', + 'a', + 'm', + 'q', + 'o', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'l': /* Specify input alphabet (labels) file. */ + if (args_info->labels_given) { + fprintf(stderr, "%s: `--labels' (`-l') option given more than once\n", PROGRAM); + } + args_info->labels_given++; + if (args_info->labels_arg) free(args_info->labels_arg); + args_info->labels_arg = strdup(val); + break; + + case 'a': /* Parse string(s) in AT&T-compatible mode. */ + if (args_info->att_mode_given) { + fprintf(stderr, "%s: `--att-mode' (`-a') option given more than once\n", PROGRAM); + } + args_info->att_mode_given++; + if (args_info->att_mode_given <= 1) + args_info->att_mode_flag = !(args_info->att_mode_flag); + break; + + case 'm': /* Output original strings in addition to label vectors. */ + if (args_info->map_mode_given) { + fprintf(stderr, "%s: `--map-mode' (`-m') option given more than once\n", PROGRAM); + } + args_info->map_mode_given++; + if (args_info->map_mode_given <= 1) + args_info->map_mode_flag = !(args_info->map_mode_flag); + break; + + case 'q': /* Suppress warnings about undefined symbols. */ + if (args_info->quiet_given) { + fprintf(stderr, "%s: `--quiet' (`-q') option given more than once\n", PROGRAM); + } + args_info->quiet_given++; + if (args_info->quiet_given <= 1) + args_info->quiet_flag = !(args_info->quiet_flag); + break; + + case 'o': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-o') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify input alphabet (labels) file. */ + else if (strcmp(olong, "labels") == 0) { + if (args_info->labels_given) { + fprintf(stderr, "%s: `--labels' (`-l') option given more than once\n", PROGRAM); + } + args_info->labels_given++; + if (args_info->labels_arg) free(args_info->labels_arg); + args_info->labels_arg = strdup(val); + } + + /* Parse string(s) in AT&T-compatible mode. */ + else if (strcmp(olong, "att-mode") == 0) { + if (args_info->att_mode_given) { + fprintf(stderr, "%s: `--att-mode' (`-a') option given more than once\n", PROGRAM); + } + args_info->att_mode_given++; + if (args_info->att_mode_given <= 1) + args_info->att_mode_flag = !(args_info->att_mode_flag); + } + + /* Output original strings in addition to label vectors. */ + else if (strcmp(olong, "map-mode") == 0) { + if (args_info->map_mode_given) { + fprintf(stderr, "%s: `--map-mode' (`-m') option given more than once\n", PROGRAM); + } + args_info->map_mode_given++; + if (args_info->map_mode_given <= 1) + args_info->map_mode_flag = !(args_info->map_mode_flag); + } + + /* Suppress warnings about undefined symbols. */ + else if (strcmp(olong, "quiet") == 0) { + if (args_info->quiet_given) { + fprintf(stderr, "%s: `--quiet' (`-q') option given more than once\n", PROGRAM); + } + args_info->quiet_given++; + if (args_info->quiet_given <= 1) + args_info->quiet_flag = !(args_info->quiet_flag); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-o') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmlabels_cmdparser.h b/gfsm/gfsm/src/programs/gfsmlabels_cmdparser.h new file mode 100644 index 0000000..71f8d95 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlabels_cmdparser.h @@ -0,0 +1,69 @@ +/* -*- Mode: C -*- + * + * File: gfsmlabels_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.06. + * + */ + +#ifndef gfsmlabels_cmdparser_h +#define gfsmlabels_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * labels_arg; /* Specify input alphabet (labels) file. (default=NULL). */ + int att_mode_flag; /* Parse string(s) in AT&T-compatible mode. (default=0). */ + int map_mode_flag; /* Output original strings in addition to label vectors. (default=0). */ + int quiet_flag; /* Suppress warnings about undefined symbols. (default=0). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int labels_given; /* Whether labels was given */ + int att_mode_given; /* Whether att-mode was given */ + int map_mode_given; /* Whether map-mode was given */ + int quiet_given; /* Whether quiet was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmlabels_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmlabels_main.c b/gfsm/gfsm/src/programs/gfsmlabels_main.c new file mode 100644 index 0000000..0e25c78 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlabels_main.c @@ -0,0 +1,208 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005-2008 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +#include <gfsm.h> + +/*-- use gnulib --*/ +#include "gnulib/getdelim.h" + +#include "gfsmlabels_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmlabels"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +FILE *outfile = NULL; + +//-- global structs +gfsmAlphabet *labels=NULL; +gfsmError *err = NULL; +gboolean att_mode = FALSE; +gboolean map_mode = FALSE; +gboolean warn_on_undef = TRUE; + +/* HACK */ +//extern ssize_t getline(char **LINEPTR, size_t *N, FILE *STREAM); + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + outfilename = args.output_arg; + + //-- open output file + if (args.output_given) { + outfilename = args.output_arg; + outfile = gfsm_open_filename(outfilename,"w",&err); + if (!outfile) { + g_printerr("%s: open failed for output file '%s': %s\n", + progname, outfilename, strerror(errno)); + exit(2); + } + } + else { + outfile = stdout; + } + + //-- labels + if (args.labels_given) { + labels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(labels, args.labels_arg, &err)) { + g_printerr("%s: load failed for labels file '%s': %s\n", + progname, args.labels_arg, err->message); + exit(3); + } + } else { + g_printerr("%s: no labels file specified!\n", progname); + exit(3); + } + + //-- mode flags + att_mode = args.att_mode_flag; + map_mode = args.map_mode_flag; + warn_on_undef = !args.quiet_flag; +} + +/*-------------------------------------------------------------------------- + * apply_labels_file() + */ +void apply_labels_file(gfsmAlphabet *labels, FILE *infile, FILE *outfile) +{ + char *str = NULL; + size_t buflen = 0; + ssize_t linelen = 0; + ssize_t i; + gfsmLabelVal lab; + gfsmLabelVector *vec = g_ptr_array_new(); + + while (!feof(infile)) { + linelen = getdelim(&str,&buflen,'\n',infile); + if (linelen<0) { break; } //-- EOF + + //-- truncate terminating newline character + if (str[linelen-1] == '\n') { str[linelen-1] = 0; } + + //-- map mode? + if (map_mode) { fprintf(outfile, "%s\t", str); } + + //-- convert + vec = gfsm_alphabet_generic_string_to_labels(labels,str,vec,warn_on_undef,att_mode); + + //-- dump labels + for (i=0; i<vec->len; i++) { + lab = GPOINTER_TO_UINT(vec->pdata[i]); + if (i>0) { fputc(' ',outfile); } + fprintf(outfile, "%d", lab); + } + fputc('\n', outfile); + } + + if (str) free(str); + if (vec) g_ptr_array_free(vec,TRUE); +} + +void apply_labels_file_0(gfsmAlphabet *labels, FILE *infile, FILE *outfile) +{ + char *str = NULL; + size_t buflen = 0; + ssize_t linelen = 0; + ssize_t i; + gfsmLabelVal lab; + char cs[2] = {'\0', '\0'}; + + while (!feof(infile)) { + /*linelen = getline(&str,&buflen,infile);*/ + linelen = getdelim(&str,&buflen,'\n',infile); + for (i=0; i < linelen; i++) { + if (isspace(str[i])) continue; + cs[0] = str[i]; + lab = gfsm_alphabet_find_label(labels,cs); + + if (lab==gfsmNoLabel) { + g_printerr("%s: Warning: no label for character '%c' -- skipping.\n", + progname, cs[0]); + continue; + } + + fprintf(outfile, "%d ", lab); + } + fputs("\n", outfile); + } + + if (str) free(str); +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + + GFSM_INIT + get_my_options(argc,argv); + + //-- process input(s) + if (args.inputs_num==0) { + apply_labels_file(labels,stdin,outfile); + } + for (i=0; i < args.inputs_num; i++) { + FILE *infile = (strcmp(args.inputs[i],"-")==0 ? stdin : fopen(args.inputs[i], "r")); + if (!infile) { + g_printerr("%s: load failed for input file '%s': %s\n", progname, args.inputs[i], strerror(errno)); + exit(255); + } + apply_labels_file(labels,infile,outfile); + if (infile != stdin) fclose(infile); + } + + + //-- cleanup + if (labels) gfsm_alphabet_free(labels); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmlookup.gog b/gfsm/gfsm/src/programs/gfsmlookup.gog new file mode 100644 index 0000000..500108f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlookup.gog @@ -0,0 +1,77 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmlookup" +#program_version "0.01" + +purpose "Apply a transducer to a linear label sequence" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "LABELID..." "Input label IDs" \ + details=" +In ASCII decimal notation. +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "fst" f "Transducer to apply (default=stdin)." \ + arg="FSTFILE" \ + default="-" \ + details=" +If unspecified, standard input will be read. +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmlookup_cmdparser.c b/gfsm/gfsm/src/programs/gfsmlookup_cmdparser.c new file mode 100644 index 0000000..ae36643 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlookup_cmdparser.c @@ -0,0 +1,473 @@ +/* -*- Mode: C -*- + * + * File: gfsmlookup_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmlookup_cmdparser gfsmlookup.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmlookup" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmlookup_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmlookup (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Apply a transducer to a linear label sequence\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... LABELID...\n", "gfsmlookup"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" LABELID... Input label IDs\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -fFSTFILE --fst=FSTFILE Transducer to apply (default=stdin).\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->fst_arg = strdup("-"); + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->fst_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "fst", 1, NULL, 'f' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'f', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'f': /* Transducer to apply (default=stdin). */ + if (args_info->fst_given) { + fprintf(stderr, "%s: `--fst' (`-f') option given more than once\n", PROGRAM); + } + args_info->fst_given++; + if (args_info->fst_arg) free(args_info->fst_arg); + args_info->fst_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Transducer to apply (default=stdin). */ + else if (strcmp(olong, "fst") == 0) { + if (args_info->fst_given) { + fprintf(stderr, "%s: `--fst' (`-f') option given more than once\n", PROGRAM); + } + args_info->fst_given++; + if (args_info->fst_arg) free(args_info->fst_arg); + args_info->fst_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmlookup_cmdparser.h b/gfsm/gfsm/src/programs/gfsmlookup_cmdparser.h new file mode 100644 index 0000000..3b20b24 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlookup_cmdparser.h @@ -0,0 +1,65 @@ +/* -*- Mode: C -*- + * + * File: gfsmlookup_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmlookup_cmdparser_h +#define gfsmlookup_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * fst_arg; /* Transducer to apply (default=stdin). (default=-). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int fst_given; /* Whether fst was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmlookup_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmlookup_main.c b/gfsm/gfsm/src/programs/gfsmlookup_main.c new file mode 100644 index 0000000..39dd8a2 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmlookup_main.c @@ -0,0 +1,125 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +#include <gfsm.h> + +#include "gfsmlookup_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmlookup"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *fstfilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fst = NULL; +gfsmError *err = NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.fst_given) fstfilename = args.fst_arg; + outfilename = args.output_arg; + + //-- load FST + fst = gfsm_automaton_new(); + if (!gfsm_automaton_load_bin_filename(fst, fstfilename, &err)) { + g_printerr("%s: load failed for FST file '%s': %s\n", progname, fstfilename, err->message); + exit(255); + } +} + +/*-------------------------------------------------------------------------- + * lookup_labels() + */ +gfsmAutomaton *lookup_labels(gfsmAutomaton *fst, int argc, char **argv) +{ + gfsmLabelVector *vec = g_ptr_array_sized_new(argc); + char *s=NULL, *tail=NULL; + gfsmLabelVal lab; + gfsmAutomaton *result = NULL; + int i; + + //-- fill input vector + for (i=0; i < argc; i++) { + for (s=argv[i], lab=strtol(s,&tail,0); s != tail; s=tail, lab=strtol(s,&tail,0)) { + g_ptr_array_add(vec, (gpointer)lab); + } + } + + //-- actual lookup + result = gfsm_automaton_lookup(fst, vec, result); + + //-- cleanup + g_ptr_array_free(vec,TRUE); + + return result; +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmAutomaton *result; + + GFSM_INIT + get_my_options(argc,argv); + + //-- process input + result = lookup_labels(fst, args.inputs_num, args.inputs); + + //-- save output + if (!gfsm_automaton_save_bin_filename(result,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fst) gfsm_automaton_free(fst); + if (result) gfsm_automaton_free(result); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmoptional.gog b/gfsm/gfsm/src/programs/gfsmoptional.gog new file mode 100644 index 0000000..3bf6102 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmoptional.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmoptional" +#program_version "0.01" + +purpose "Make a an automaton optional" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmoptional_cmdparser.c b/gfsm/gfsm/src/programs/gfsmoptional_cmdparser.c new file mode 100644 index 0000000..0f90ca4 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmoptional_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmoptional_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmoptional_cmdparser gfsmoptional.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmoptional" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmoptional_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmoptional (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Make a an automaton optional\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmoptional"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmoptional_cmdparser.h b/gfsm/gfsm/src/programs/gfsmoptional_cmdparser.h new file mode 100644 index 0000000..566495e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmoptional_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmoptional_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmoptional_cmdparser_h +#define gfsmoptional_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmoptional_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmoptional_main.c b/gfsm/gfsm/src/programs/gfsmoptional_main.c new file mode 100644 index 0000000..73c442c --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmoptional_main.c @@ -0,0 +1,101 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmoptional_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmoptional"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * Utilities + *--------------------------------------------------------------------------*/ +#define bool2char(b) (b ? 'y' : 'n') + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + + GFSM_INIT + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(2); + } + + //-- make optional + gfsm_automaton_optional(fsm); + + //-- store automaton + if (!gfsm_automaton_save_bin_filename(fsm,args.output_arg,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, args.output_arg, err->message); + exit(4); + } + + //-- cleanup + gfsm_automaton_free(fsm); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmprint.gog b/gfsm/gfsm/src/programs/gfsmprint.gog new file mode 100644 index 0000000..4a01ef2 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmprint.gog @@ -0,0 +1,80 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmprint" +#program_version "0.01" + +purpose "Convert binary format gfsm files to text" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +argument "TXTFILE" "Output text file" \ + details=" +If unspecified, output will be written to standard output. +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "ilabels" i "Specify input (lower) labels file." \ + arg="LABELS" + +string "olabels" o "Specify output (upper) labels file." \ + arg="LABELS" + +string "slabels" s "Specify state labels file." \ + arg="LABELS" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="0" \ + details=" +Specify zlib compression level of output file. -1 indicates +the library default compression level, 0 (default) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmprint_cmdparser.c b/gfsm/gfsm/src/programs/gfsmprint_cmdparser.c new file mode 100644 index 0000000..253eb1d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmprint_cmdparser.c @@ -0,0 +1,498 @@ +/* -*- Mode: C -*- + * + * File: gfsmprint_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmprint_cmdparser gfsmprint.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmprint" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmprint_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmprint (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Convert binary format gfsm files to text\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE TXTFILE\n", "gfsmprint"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + printf(" TXTFILE Output text file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -iLABELS --ilabels=LABELS Specify input (lower) labels file.\n"); + printf(" -oLABELS --olabels=LABELS Specify output (upper) labels file.\n"); + printf(" -sLABELS --slabels=LABELS Specify state labels file.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->ilabels_arg = NULL; + args_info->olabels_arg = NULL; + args_info->slabels_arg = NULL; + args_info->compress_arg = 0; +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->ilabels_given = 0; + args_info->olabels_given = 0; + args_info->slabels_given = 0; + args_info->compress_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "ilabels", 1, NULL, 'i' }, + { "olabels", 1, NULL, 'o' }, + { "slabels", 1, NULL, 's' }, + { "compress", 1, NULL, 'z' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'i', ':', + 'o', ':', + 's', ':', + 'z', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'i': /* Specify input (lower) labels file. */ + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + break; + + case 'o': /* Specify output (upper) labels file. */ + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + break; + + case 's': /* Specify state labels file. */ + if (args_info->slabels_given) { + fprintf(stderr, "%s: `--slabels' (`-s') option given more than once\n", PROGRAM); + } + args_info->slabels_given++; + if (args_info->slabels_arg) free(args_info->slabels_arg); + args_info->slabels_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify input (lower) labels file. */ + else if (strcmp(olong, "ilabels") == 0) { + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + } + + /* Specify output (upper) labels file. */ + else if (strcmp(olong, "olabels") == 0) { + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + } + + /* Specify state labels file. */ + else if (strcmp(olong, "slabels") == 0) { + if (args_info->slabels_given) { + fprintf(stderr, "%s: `--slabels' (`-s') option given more than once\n", PROGRAM); + } + args_info->slabels_given++; + if (args_info->slabels_arg) free(args_info->slabels_arg); + args_info->slabels_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmprint_cmdparser.h b/gfsm/gfsm/src/programs/gfsmprint_cmdparser.h new file mode 100644 index 0000000..0b06787 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmprint_cmdparser.h @@ -0,0 +1,67 @@ +/* -*- Mode: C -*- + * + * File: gfsmprint_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmprint_cmdparser_h +#define gfsmprint_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * ilabels_arg; /* Specify input (lower) labels file. (default=NULL). */ + char * olabels_arg; /* Specify output (upper) labels file. (default=NULL). */ + char * slabels_arg; /* Specify state labels file. (default=NULL). */ + int compress_arg; /* Specify compression level of output file. (default=0). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int ilabels_given; /* Whether ilabels was given */ + int olabels_given; /* Whether olabels was given */ + int slabels_given; /* Whether slabels was given */ + int compress_given; /* Whether compress was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmprint_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmprint_main.c b/gfsm/gfsm/src/programs/gfsmprint_main.c new file mode 100644 index 0000000..fe6d7f3 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmprint_main.c @@ -0,0 +1,125 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmprint_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmprint"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; +gfsmAlphabet *ilabels=NULL, *olabels=NULL, *slabels=NULL; +gfsmError *err = NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + if (args.inputs_num > 1) outfilename = args.inputs[1]; + + //-- labels: input + if (args.ilabels_given) { + ilabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(ilabels,args.ilabels_arg,&err)) { + g_printerr("%s: load failed for input-labels file '%s': %s\n", + progname, args.ilabels_arg, err->message); + exit(2); + } + } + //-- labels: output + if (args.olabels_given) { + olabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(olabels,args.olabels_arg,&err)) { + g_printerr("%s: load failed for output-labels file '%s': %s\n", + progname, args.olabels_arg, err->message); + exit(2); + } + } + //-- labels: state + if (args.slabels_given) { + slabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(slabels,args.slabels_arg,&err)) { + g_printerr("%s: load failed for state-labels file '%s': %s\n", + progname, args.slabels_arg, err->message); + exit(2); + } + } + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(3); + } + + //-- print automaton + if (!gfsm_automaton_print_filename_full(fsm,outfilename,ilabels,olabels,slabels,args.compress_arg,&err)) { + g_printerr("%s: print failed to '%s': %s\n", progname, outfilename, err->message); + exit(3); + } + + //-- cleanup + if (ilabels) gfsm_alphabet_free(ilabels); + if (olabels) gfsm_alphabet_free(olabels); + if (slabels) gfsm_alphabet_free(slabels); + gfsm_automaton_free(fsm); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmproduct.gog b/gfsm/gfsm/src/programs/gfsmproduct.gog new file mode 100644 index 0000000..120c823 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproduct.gog @@ -0,0 +1,78 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmproduct" +#program_version "0.01" + +purpose "Compute Cartesian product of finite state acceptors" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "LOWER" "Stored binary gfsm file" \ + required=1 \ + details=" +FSM accepting the lower side of the output FST. +Required argument. +" + +argument "UPPER" "Stored binary gfsm file" \ + details=" +FSM accepting the upper side of the output FST. +If unspecified, standard input will be read. +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmproduct_cmdparser.c b/gfsm/gfsm/src/programs/gfsmproduct_cmdparser.c new file mode 100644 index 0000000..27fd759 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproduct_cmdparser.c @@ -0,0 +1,450 @@ +/* -*- Mode: C -*- + * + * File: gfsmproduct_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmproduct_cmdparser gfsmproduct.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmproduct" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmproduct_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmproduct (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute Cartesian product of finite state acceptors\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... LOWER UPPER\n", "gfsmproduct"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" LOWER Stored binary gfsm file\n"); + printf(" UPPER Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmproduct_cmdparser.h b/gfsm/gfsm/src/programs/gfsmproduct_cmdparser.h new file mode 100644 index 0000000..527630e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproduct_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmproduct_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmproduct_cmdparser_h +#define gfsmproduct_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmproduct_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmproduct_main.c b/gfsm/gfsm/src/programs/gfsmproduct_main.c new file mode 100644 index 0000000..446e875 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproduct_main.c @@ -0,0 +1,123 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmproduct_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmproduct"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmOut=NULL, *fsmIn=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- require at least one file argument + if (args.inputs_num < 1) { + cmdline_parser_print_help(); + exit(2); + } + + //-- output + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsmIn = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * compute_operation() + * + utility routine + */ +void compute_operation(const char *infilename) +{ + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsmIn,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute product + if (fsmOut == NULL) { + fsmOut = fsmIn; + fsmIn = gfsm_automaton_new(); + } else { + gfsm_automaton_product2(fsmOut,fsmIn); //-- dual-destructive version + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + + GFSM_INIT + + get_my_options(argc,argv); + + for (i = 0; i < args.inputs_num && i < 2; i++) { + compute_operation(args.inputs[i]); + } + if (args.inputs_num == 1) compute_operation("-"); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmOut,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsmIn) gfsm_automaton_free(fsmIn); + if (fsmOut) gfsm_automaton_free(fsmOut); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmproject.gog b/gfsm/gfsm/src/programs/gfsmproject.gog new file mode 100644 index 0000000..42fcb6c --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproject.gog @@ -0,0 +1,76 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmproject" +#program_version "0.01" + +purpose "Project one side of finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +flag "first" 1 "Project lower side [default]." +flag - i "Project input tape (alias for -1)" + +flag "second" 2 "Project upper side." +flag - o "Project output side (alias for -2)" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmproject_cmdparser.c b/gfsm/gfsm/src/programs/gfsmproject_cmdparser.c new file mode 100644 index 0000000..5905210 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproject_cmdparser.c @@ -0,0 +1,523 @@ +/* -*- Mode: C -*- + * + * File: gfsmproject_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmproject_cmdparser gfsmproject.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmproject" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmproject_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmproject (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Project one side of finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmproject"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -1 --first Project lower side [default].\n"); + printf(" -i Project input tape (alias for -1)\n"); + printf(" -2 --second Project upper side.\n"); + printf(" -o Project output side (alias for -2)\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->first_flag = 0; + args_info->i_flag = 0; + args_info->second_flag = 0; + args_info->o_flag = 0; + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->first_given = 0; + args_info->i_given = 0; + args_info->second_given = 0; + args_info->o_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "first", 0, NULL, '1' }, + { "second", 0, NULL, '2' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + '1', + 'i', + '2', + 'o', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case '1': /* Project lower side [default]. */ + if (args_info->first_given) { + fprintf(stderr, "%s: `--first' (`-1') option given more than once\n", PROGRAM); + } + args_info->first_given++; + if (args_info->first_given <= 1) + args_info->first_flag = !(args_info->first_flag); + break; + + case 'i': /* Project input tape (alias for -1) */ + if (args_info->i_given) { + fprintf(stderr, "%s: (`-i') option given more than once\n", PROGRAM); + } + args_info->i_given++; + if (args_info->i_given <= 1) + args_info->i_flag = !(args_info->i_flag); + break; + + case '2': /* Project upper side. */ + if (args_info->second_given) { + fprintf(stderr, "%s: `--second' (`-2') option given more than once\n", PROGRAM); + } + args_info->second_given++; + if (args_info->second_given <= 1) + args_info->second_flag = !(args_info->second_flag); + break; + + case 'o': /* Project output side (alias for -2) */ + if (args_info->o_given) { + fprintf(stderr, "%s: (`-o') option given more than once\n", PROGRAM); + } + args_info->o_given++; + if (args_info->o_given <= 1) + args_info->o_flag = !(args_info->o_flag); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Project lower side [default]. */ + else if (strcmp(olong, "first") == 0) { + if (args_info->first_given) { + fprintf(stderr, "%s: `--first' (`-1') option given more than once\n", PROGRAM); + } + args_info->first_given++; + if (args_info->first_given <= 1) + args_info->first_flag = !(args_info->first_flag); + } + + /* Project upper side. */ + else if (strcmp(olong, "second") == 0) { + if (args_info->second_given) { + fprintf(stderr, "%s: `--second' (`-2') option given more than once\n", PROGRAM); + } + args_info->second_given++; + if (args_info->second_given <= 1) + args_info->second_flag = !(args_info->second_flag); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmproject_cmdparser.h b/gfsm/gfsm/src/programs/gfsmproject_cmdparser.h new file mode 100644 index 0000000..e7fbb80 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproject_cmdparser.h @@ -0,0 +1,71 @@ +/* -*- Mode: C -*- + * + * File: gfsmproject_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmproject_cmdparser_h +#define gfsmproject_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int first_flag; /* Project lower side [default]. (default=0). */ + int i_flag; /* Project input tape (alias for -1) (default=0). */ + int second_flag; /* Project upper side. (default=0). */ + int o_flag; /* Project output side (alias for -2) (default=0). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int first_given; /* Whether first was given */ + int i_given; /* Whether - was given */ + int second_given; /* Whether second was given */ + int o_given; /* Whether - was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmproject_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmproject_main.c b/gfsm/gfsm/src/programs/gfsmproject_main.c new file mode 100644 index 0000000..528b456 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmproject_main.c @@ -0,0 +1,100 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmproject_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmproject"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs & vars +gfsmAutomaton *fsm; +gfsmLabelSide which = gfsmLSLower; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- which? + if (args.second_given || args.o_given) which = gfsmLSUpper; + else which = gfsmLSLower; + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- invert + gfsm_automaton_project(fsm,which); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmrenumber.gog b/gfsm/gfsm/src/programs/gfsmrenumber.gog new file mode 100644 index 0000000..df2d0b6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrenumber.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmrenumber" +#program_version "0.01" + +purpose "Renumber states in finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmrenumber_cmdparser.c b/gfsm/gfsm/src/programs/gfsmrenumber_cmdparser.c new file mode 100644 index 0000000..a058eb2 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrenumber_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmrenumber_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmrenumber_cmdparser gfsmrenumber.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmrenumber" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmrenumber_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmrenumber (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Renumber states in finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmrenumber"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmrenumber_cmdparser.h b/gfsm/gfsm/src/programs/gfsmrenumber_cmdparser.h new file mode 100644 index 0000000..08db747 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrenumber_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmrenumber_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmrenumber_cmdparser_h +#define gfsmrenumber_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmrenumber_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmrenumber_main.c b/gfsm/gfsm/src/programs/gfsmrenumber_main.c new file mode 100644 index 0000000..7bf7a59 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrenumber_main.c @@ -0,0 +1,95 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmrenumber_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmrenumber"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- renumber + gfsm_automaton_renumber_states(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmreplace.gog b/gfsm/gfsm/src/programs/gfsmreplace.gog new file mode 100644 index 0000000..466526d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreplace.gog @@ -0,0 +1,79 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmreplace" +#program_version "0.01" + +purpose "Replace some automaton arcs with a whole automaton" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "replacement" r "Replacement automaton (binary gfsm file)" \ + arg="FSMFILE" \ + default="-" \ + details=" +If unspecified, standard input will be read. +" + +int "lower" l "Lower label to replace (default=any)" \ + arg="LABEL" + +int "upper" u "Upper label to replace (default=any)" \ + arg="LABEL" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmreplace_cmdparser.c b/gfsm/gfsm/src/programs/gfsmreplace_cmdparser.c new file mode 100644 index 0000000..652c7a7 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreplace_cmdparser.c @@ -0,0 +1,514 @@ +/* -*- Mode: C -*- + * + * File: gfsmreplace_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmreplace_cmdparser gfsmreplace.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmreplace" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmreplace_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmreplace (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Replace some automaton arcs with a whole automaton\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... [FILES]...\n", "gfsmreplace"); + + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -rFSMFILE --replacement=FSMFILE Replacement automaton (binary gfsm file)\n"); + printf(" -lLABEL --lower=LABEL Lower label to replace (default=any)\n"); + printf(" -uLABEL --upper=LABEL Upper label to replace (default=any)\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->replacement_arg = strdup("-"); + args_info->lower_arg = 0; + args_info->upper_arg = 0; + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->replacement_given = 0; + args_info->lower_given = 0; + args_info->upper_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "replacement", 1, NULL, 'r' }, + { "lower", 1, NULL, 'l' }, + { "upper", 1, NULL, 'u' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'r', ':', + 'l', ':', + 'u', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'r': /* Replacement automaton (binary gfsm file) */ + if (args_info->replacement_given) { + fprintf(stderr, "%s: `--replacement' (`-r') option given more than once\n", PROGRAM); + } + args_info->replacement_given++; + if (args_info->replacement_arg) free(args_info->replacement_arg); + args_info->replacement_arg = strdup(val); + break; + + case 'l': /* Lower label to replace (default=any) */ + if (args_info->lower_given) { + fprintf(stderr, "%s: `--lower' (`-l') option given more than once\n", PROGRAM); + } + args_info->lower_given++; + args_info->lower_arg = (int)atoi(val); + break; + + case 'u': /* Upper label to replace (default=any) */ + if (args_info->upper_given) { + fprintf(stderr, "%s: `--upper' (`-u') option given more than once\n", PROGRAM); + } + args_info->upper_given++; + args_info->upper_arg = (int)atoi(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Replacement automaton (binary gfsm file) */ + else if (strcmp(olong, "replacement") == 0) { + if (args_info->replacement_given) { + fprintf(stderr, "%s: `--replacement' (`-r') option given more than once\n", PROGRAM); + } + args_info->replacement_given++; + if (args_info->replacement_arg) free(args_info->replacement_arg); + args_info->replacement_arg = strdup(val); + } + + /* Lower label to replace (default=any) */ + else if (strcmp(olong, "lower") == 0) { + if (args_info->lower_given) { + fprintf(stderr, "%s: `--lower' (`-l') option given more than once\n", PROGRAM); + } + args_info->lower_given++; + args_info->lower_arg = (int)atoi(val); + } + + /* Upper label to replace (default=any) */ + else if (strcmp(olong, "upper") == 0) { + if (args_info->upper_given) { + fprintf(stderr, "%s: `--upper' (`-u') option given more than once\n", PROGRAM); + } + args_info->upper_given++; + args_info->upper_arg = (int)atoi(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmreplace_cmdparser.h b/gfsm/gfsm/src/programs/gfsmreplace_cmdparser.h new file mode 100644 index 0000000..f7c704d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreplace_cmdparser.h @@ -0,0 +1,69 @@ +/* -*- Mode: C -*- + * + * File: gfsmreplace_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmreplace_cmdparser_h +#define gfsmreplace_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * replacement_arg; /* Replacement automaton (binary gfsm file) (default=-). */ + int lower_arg; /* Lower label to replace (default=any) (default=0). */ + int upper_arg; /* Upper label to replace (default=any) (default=0). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int replacement_given; /* Whether replacement was given */ + int lower_given; /* Whether lower was given */ + int upper_given; /* Whether upper was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmreplace_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmreplace_main.c b/gfsm/gfsm/src/programs/gfsmreplace_main.c new file mode 100644 index 0000000..c2b1107 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreplace_main.c @@ -0,0 +1,108 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +#include <gfsm.h> + +#include "gfsmreplace_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmreplace"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *replfilename = "-"; +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *repl = NULL, *fsm=NULL; +gfsmError *err = NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num) infilename = args.inputs[0]; + if (args.replacement_given) replfilename = args.replacement_arg; + outfilename = args.output_arg; + + //-- load replacement automaton + repl = gfsm_automaton_new(); + if (!gfsm_automaton_load_bin_filename(repl, replfilename, &err)) { + g_printerr("%s: load failed for FST file '%s': %s\n", progname, replfilename, err->message); + exit(255); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + get_my_options(argc,argv); + + //-- load input automaton + fsm = gfsm_automaton_new(); + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- perform replacement + gfsm_automaton_replace(fsm, + (args.lower_given ? args.lower_arg : gfsmNoLabel), + (args.upper_given ? args.upper_arg : gfsmNoLabel), + repl); + + //-- save output + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (repl) gfsm_automaton_free(repl); + if (fsm) gfsm_automaton_free(fsm); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmreverse.gog b/gfsm/gfsm/src/programs/gfsmreverse.gog new file mode 100644 index 0000000..f67c417 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreverse.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmreverse" +#program_version "0.01" + +purpose "Reverse a finite state machine" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read. +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmreverse_cmdparser.c b/gfsm/gfsm/src/programs/gfsmreverse_cmdparser.c new file mode 100644 index 0000000..f992776 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreverse_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmreverse_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmreverse_cmdparser gfsmreverse.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmreverse" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmreverse_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmreverse (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Reverse a finite state machine\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmreverse"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmreverse_cmdparser.h b/gfsm/gfsm/src/programs/gfsmreverse_cmdparser.h new file mode 100644 index 0000000..81ed264 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreverse_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmreverse_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmreverse_cmdparser_h +#define gfsmreverse_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmreverse_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmreverse_main.c b/gfsm/gfsm/src/programs/gfsmreverse_main.c new file mode 100644 index 0000000..e2b24f1 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmreverse_main.c @@ -0,0 +1,101 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmreverse_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmreverse"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs & vars +gfsmAutomaton *fsm; +int which = 1; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + + GFSM_INIT + + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- invert + gfsm_automaton_reverse(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmrmepsilon.gog b/gfsm/gfsm/src/programs/gfsmrmepsilon.gog new file mode 100644 index 0000000..e1b78d6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrmepsilon.gog @@ -0,0 +1,80 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmrmepsilon" +#program_version "0.01" + +purpose "Remove epsilon arcs from finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +=over 4 + +=item + +No negative-cost epsilon cycles are allowed in the input automaton. + +=item + +Probably many more. + +=back + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmrmepsilon_cmdparser.c b/gfsm/gfsm/src/programs/gfsmrmepsilon_cmdparser.c new file mode 100644 index 0000000..1f4351f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrmepsilon_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmrmepsilon_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmrmepsilon_cmdparser gfsmrmepsilon.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmrmepsilon" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmrmepsilon_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmrmepsilon (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Remove epsilon arcs from finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmrmepsilon"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmrmepsilon_cmdparser.h b/gfsm/gfsm/src/programs/gfsmrmepsilon_cmdparser.h new file mode 100644 index 0000000..e9deda5 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrmepsilon_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmrmepsilon_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmrmepsilon_cmdparser_h +#define gfsmrmepsilon_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmrmepsilon_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmrmepsilon_main.c b/gfsm/gfsm/src/programs/gfsmrmepsilon_main.c new file mode 100644 index 0000000..ec3e899 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmrmepsilon_main.c @@ -0,0 +1,95 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmrmepsilon_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmrmepsilon"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fsm; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) infilename = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsm = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmError *err = NULL; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- rmepsilon + gfsm_automaton_rmepsilon(fsm); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsm,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsm) gfsm_automaton_free(fsm); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmsigma.gog b/gfsm/gfsm/src/programs/gfsmsigma.gog new file mode 100644 index 0000000..68b7243 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmsigma.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmsigma" +#program_version "0.01" + +purpose "Create a single-state alphabet acceptor" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "LABFILE" "Alphabet file" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmsigma_cmdparser.c b/gfsm/gfsm/src/programs/gfsmsigma_cmdparser.c new file mode 100644 index 0000000..7cf017f --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmsigma_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmsigma_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmsigma_cmdparser gfsmsigma.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmsigma" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmsigma_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmsigma (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Create a single-state alphabet acceptor\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... LABFILE\n", "gfsmsigma"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" LABFILE Alphabet file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmsigma_cmdparser.h b/gfsm/gfsm/src/programs/gfsmsigma_cmdparser.h new file mode 100644 index 0000000..4a7832c --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmsigma_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmsigma_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmsigma_cmdparser_h +#define gfsmsigma_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmsigma_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmsigma_main.c b/gfsm/gfsm/src/programs/gfsmsigma_main.c new file mode 100644 index 0000000..ce5c59d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmsigma_main.c @@ -0,0 +1,100 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmsigma_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmsigma"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *abetname = "-"; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmOut=NULL; +gfsmAlphabet *abet=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- output + if (args.inputs_num) abetname = args.inputs[0]; + if (args.output_arg) outfilename = args.output_arg; + + //-- initialize automaton + fsmOut = gfsm_automaton_new(); +} + + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + GFSM_INIT + + get_my_options(argc,argv); + + //-- load alphabet + abet = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(abet, abetname, &err)) { + g_printerr("%s: load failed for alphabet file '%s': %s\n", + progname, abetname, err->message); + exit(2); + } + + //-- compute operation + gfsm_automaton_sigma(fsmOut,abet); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmOut,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (abet) gfsm_alphabet_free(abet); + if (fsmOut) gfsm_automaton_free(fsmOut); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmstrings.gog b/gfsm/gfsm/src/programs/gfsmstrings.gog new file mode 100644 index 0000000..5672f36 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmstrings.gog @@ -0,0 +1,76 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmstrings" +#program_version "0.01" + +purpose "Serialize binary acyclic gfsm files to lists of strings" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +#details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/gfsmrc" +#rcfile "~/.gfsmrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE" "Stored binary gfsm file" \ + details=" +If unspecified, standard input will be read. +" + +#----------------------------------------------------------------------------- +# Common Options +#----------------------------------------------------------------------------- +group "Common Options" + +string "ilabels" i "Specify input (lower) labels file." \ + arg="LABELS" + +string "olabels" o "Specify output (upper) labels file." \ + arg="LABELS" + +flag "att" a "Output in AT&T regex format." \ + default=0 + +flag "viterbi" v "Treat input automaton as a Viterbi trellis." \ + default=0 + +string "output" F "Output file." \ + arg="TXTFILE" \ + details=" +Default behavior is to write to stdout. +" + + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils>, +" diff --git a/gfsm/gfsm/src/programs/gfsmstrings_cmdparser.c b/gfsm/gfsm/src/programs/gfsmstrings_cmdparser.c new file mode 100644 index 0000000..6f2c30b --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmstrings_cmdparser.c @@ -0,0 +1,525 @@ +/* -*- Mode: C -*- + * + * File: gfsmstrings_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmstrings_cmdparser gfsmstrings.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmstrings" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmstrings_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmstrings (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Serialize binary acyclic gfsm files to lists of strings\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE\n", "gfsmstrings"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE Stored binary gfsm file\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf("\n"); + printf(" Common Options:\n"); + printf(" -iLABELS --ilabels=LABELS Specify input (lower) labels file.\n"); + printf(" -oLABELS --olabels=LABELS Specify output (upper) labels file.\n"); + printf(" -a --att Output in AT&T regex format.\n"); + printf(" -v --viterbi Treat input automaton as a Viterbi trellis.\n"); + printf(" -FTXTFILE --output=TXTFILE Output file.\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->ilabels_arg = NULL; + args_info->olabels_arg = NULL; + args_info->att_flag = 0; + args_info->viterbi_flag = 0; + args_info->output_arg = NULL; +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->ilabels_given = 0; + args_info->olabels_given = 0; + args_info->att_given = 0; + args_info->viterbi_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "ilabels", 1, NULL, 'i' }, + { "olabels", 1, NULL, 'o' }, + { "att", 0, NULL, 'a' }, + { "viterbi", 0, NULL, 'v' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'i', ':', + 'o', ':', + 'a', + 'v', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'i': /* Specify input (lower) labels file. */ + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + break; + + case 'o': /* Specify output (upper) labels file. */ + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + break; + + case 'a': /* Output in AT&T regex format. */ + if (args_info->att_given) { + fprintf(stderr, "%s: `--att' (`-a') option given more than once\n", PROGRAM); + } + args_info->att_given++; + if (args_info->att_given <= 1) + args_info->att_flag = !(args_info->att_flag); + break; + + case 'v': /* Treat input automaton as a Viterbi trellis. */ + if (args_info->viterbi_given) { + fprintf(stderr, "%s: `--viterbi' (`-v') option given more than once\n", PROGRAM); + } + args_info->viterbi_given++; + if (args_info->viterbi_given <= 1) + args_info->viterbi_flag = !(args_info->viterbi_flag); + break; + + case 'F': /* Output file. */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify input (lower) labels file. */ + else if (strcmp(olong, "ilabels") == 0) { + if (args_info->ilabels_given) { + fprintf(stderr, "%s: `--ilabels' (`-i') option given more than once\n", PROGRAM); + } + args_info->ilabels_given++; + if (args_info->ilabels_arg) free(args_info->ilabels_arg); + args_info->ilabels_arg = strdup(val); + } + + /* Specify output (upper) labels file. */ + else if (strcmp(olong, "olabels") == 0) { + if (args_info->olabels_given) { + fprintf(stderr, "%s: `--olabels' (`-o') option given more than once\n", PROGRAM); + } + args_info->olabels_given++; + if (args_info->olabels_arg) free(args_info->olabels_arg); + args_info->olabels_arg = strdup(val); + } + + /* Output in AT&T regex format. */ + else if (strcmp(olong, "att") == 0) { + if (args_info->att_given) { + fprintf(stderr, "%s: `--att' (`-a') option given more than once\n", PROGRAM); + } + args_info->att_given++; + if (args_info->att_given <= 1) + args_info->att_flag = !(args_info->att_flag); + } + + /* Treat input automaton as a Viterbi trellis. */ + else if (strcmp(olong, "viterbi") == 0) { + if (args_info->viterbi_given) { + fprintf(stderr, "%s: `--viterbi' (`-v') option given more than once\n", PROGRAM); + } + args_info->viterbi_given++; + if (args_info->viterbi_given <= 1) + args_info->viterbi_flag = !(args_info->viterbi_flag); + } + + /* Output file. */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmstrings_cmdparser.h b/gfsm/gfsm/src/programs/gfsmstrings_cmdparser.h new file mode 100644 index 0000000..51e085e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmstrings_cmdparser.h @@ -0,0 +1,69 @@ +/* -*- Mode: C -*- + * + * File: gfsmstrings_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmstrings_cmdparser_h +#define gfsmstrings_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * ilabels_arg; /* Specify input (lower) labels file. (default=NULL). */ + char * olabels_arg; /* Specify output (upper) labels file. (default=NULL). */ + int att_flag; /* Output in AT&T regex format. (default=0). */ + int viterbi_flag; /* Treat input automaton as a Viterbi trellis. (default=0). */ + char * output_arg; /* Output file. (default=NULL). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int ilabels_given; /* Whether ilabels was given */ + int olabels_given; /* Whether olabels was given */ + int att_given; /* Whether att was given */ + int viterbi_given; /* Whether viterbi was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmstrings_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmstrings_main.c b/gfsm/gfsm/src/programs/gfsmstrings_main.c new file mode 100644 index 0000000..4b561e6 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmstrings_main.c @@ -0,0 +1,153 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmstrings_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmstrings"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename = "-"; +const char *outfilename = "-"; +FILE *outfile = NULL; + +//-- global structs +gfsmAutomaton *fsm; +gfsmAlphabet *ilabels=NULL, *olabels=NULL; +gfsmError *err = NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.inputs_num > 0) infilename = args.inputs[0]; + if (args.output_given) outfilename = args.output_arg; + + //-- labels: input + if (args.ilabels_given) { + ilabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(ilabels,args.ilabels_arg,&err)) { + g_printerr("%s: load failed for input-labels file '%s': %s\n", + progname, args.ilabels_arg, (err ? err->message : "?")); + exit(2); + } + } + //-- labels: output + if (args.olabels_given) { + olabels = gfsm_string_alphabet_new(); + if (!gfsm_alphabet_load_filename(olabels,args.olabels_arg,&err)) { + g_printerr("%s: load failed for output-labels file '%s': %s\n", + progname, args.olabels_arg, (err ? err->message : "?")); + exit(2); + } + } + + //-- initialize fsm + fsm = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmSet *paths = NULL; + GSList *strings = NULL; + get_my_options(argc,argv); + + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsm,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, (err ? err->message : "?")); + exit(3); + } + + //-- sanity check + if (gfsm_automaton_is_cyclic(fsm)) { + g_printerr("%s: input automaton must be acyclic!\n", progname); + exit(255); + } + + //-- open output file + outfile = gfsm_open_filename(outfilename, "w", &err); + if (!outfile) { + g_printerr("%s: %s\n", progname, (err ? err->message : "?")); + exit(4); + } + + + //-- get & stringify full paths + if (args.viterbi_flag) { + //-- serialize Viterbi trellis automaton + paths = gfsm_viterbi_trellis_paths_full(fsm, NULL, gfsmLSBoth); + } + else { + //-- serialize "normal" automaton + paths = gfsm_automaton_paths_full(fsm, NULL, gfsmLSBoth); + } + strings = gfsm_paths_to_strings(paths, + ilabels, + olabels, + fsm->sr, + TRUE, + args.att_given, + NULL); + while (strings) { + //-- pop first datum + char *s = (char *)strings->data; + strings = g_slist_delete_link(strings,strings); + + //-- print string + fputs(s, outfile); + fputc('\n', outfile); + + g_free(s); + } + + //-- cleanup + if (paths) gfsm_set_free(paths); + if (ilabels) gfsm_alphabet_free(ilabels); + if (olabels) gfsm_alphabet_free(olabels); + if (fsm) gfsm_automaton_free(fsm); + + if (outfile != stdout) fclose(outfile); + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmunion.gog b/gfsm/gfsm/src/programs/gfsmunion.gog new file mode 100644 index 0000000..96d84e2 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmunion.gog @@ -0,0 +1,70 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmunion" +#program_version "0.01" + +purpose "Compute union of finite state machines" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "BINFILE(s)" "Stored binary gfsm file(s)" \ + details=" +If unspecified, standard input will be read +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +None known. + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmunion_cmdparser.c b/gfsm/gfsm/src/programs/gfsmunion_cmdparser.c new file mode 100644 index 0000000..31aafa8 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmunion_cmdparser.c @@ -0,0 +1,449 @@ +/* -*- Mode: C -*- + * + * File: gfsmunion_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmunion_cmdparser gfsmunion.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmunion" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmunion_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmunion (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" Compute union of finite state machines\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... BINFILE(s)\n", "gfsmunion"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" BINFILE(s) Stored binary gfsm file(s)\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmunion_cmdparser.h b/gfsm/gfsm/src/programs/gfsmunion_cmdparser.h new file mode 100644 index 0000000..6086492 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmunion_cmdparser.h @@ -0,0 +1,63 @@ +/* -*- Mode: C -*- + * + * File: gfsmunion_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmunion_cmdparser_h +#define gfsmunion_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmunion_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmunion_main.c b/gfsm/gfsm/src/programs/gfsmunion_main.c new file mode 100644 index 0000000..dd7998d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmunion_main.c @@ -0,0 +1,123 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <gfsm.h> + +#include "gfsmunion_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmunion"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *infilename; +const char *outfilename = "-"; + +//-- global structs etc. +gfsmError *err = NULL; +gfsmAutomaton *fsmUnion=NULL, *fsmIn=NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- require at least one file argument + if (args.inputs_num < 1) { + cmdline_parser_print_help(); + exit(2); + } + + //-- output + if (args.output_arg) outfilename = args.output_arg; + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- initialize automaton + fsmIn = gfsm_automaton_new(); +} + +/*-------------------------------------------------------------------------- + * compute_union() + * + utility routine + */ +void compute_union(const char *infilename) +{ + //-- load automaton + if (!gfsm_automaton_load_bin_filename(fsmIn,infilename,&err)) { + g_printerr("%s: load failed for '%s': %s\n", progname, infilename, err->message); + exit(255); + } + + //-- compute union + if (fsmUnion == NULL) { + fsmUnion = fsmIn; + fsmIn = gfsm_automaton_new(); + } else { + gfsm_automaton_union(fsmUnion,fsmIn); + } +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + int i; + + GFSM_INIT + + get_my_options(argc,argv); + + for (i = 0; i < args.inputs_num; i++) { + compute_union(args.inputs[i]); + } + if (args.inputs_num == 1) compute_union("-"); + + //-- spew automaton + if (!gfsm_automaton_save_bin_filename(fsmUnion,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fsmIn) gfsm_automaton_free(fsmIn); + if (fsmUnion) gfsm_automaton_free(fsmUnion); + + GFSM_FINISH + + return 0; +} diff --git a/gfsm/gfsm/src/programs/gfsmview.sh b/gfsm/gfsm/src/programs/gfsmview.sh new file mode 100755 index 0000000..d8df9c2 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmview.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +GFSMDRAW=./gfsmdraw +DOTGV=dotgv.sh + +exec $GFSMDRAW "$@" | $DOTGV diff --git a/gfsm/gfsm/src/programs/gfsmviterbi.gog b/gfsm/gfsm/src/programs/gfsmviterbi.gog new file mode 100644 index 0000000..827d26e --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmviterbi.gog @@ -0,0 +1,88 @@ +# -*- Mode: Shell-Script -*- +# +# Getopt::Gen specification +#----------------------------------------------------------------------------- +program "gfsmviterbi" +#program_version "0.01" + +purpose "EXPERIMENTAL: Compute Viterbi trellis for a linear label sequence in a transducer" +author "Bryan Jurish <moocow@ling.uni-potsdam.de>" +on_reparse "warn" + +#----------------------------------------------------------------------------- +# Details +#----------------------------------------------------------------------------- +details "" + +#----------------------------------------------------------------------------- +# Files +#----------------------------------------------------------------------------- +#rcfile "/etc/mootrc" +#rcfile "~/.mootrc" + +#----------------------------------------------------------------------------- +# Arguments +#----------------------------------------------------------------------------- +argument "LABELID..." "Input label IDs" \ + details=" +In ASCII decimal notation. +" + +#----------------------------------------------------------------------------- +# Options +#----------------------------------------------------------------------------- +#group "Basic Options" + +string "fst" f "Weighted transducer to apply (default=stdin)." \ + arg="FSTFILE" \ + default="-" \ + details=" +If unspecified, standard input will be read. +" + +int "compress" z "Specify compression level of output file." \ + arg="LEVEL" \ + default="-1" \ + details=" +Specify zlib compression level of output file. -1 (default) indicates +the default compression level, 0 (zero) indicates no zlib compression at all, +and 9 indicates the best possible compression. +" + +string "output" F "Specifiy output file (default=stdout)." \ + arg="FILE" \ + default="-" + +#----------------------------------------------------------------------------- +# Addenda +#----------------------------------------------------------------------------- +#addenda "" + +#----------------------------------------------------------------------------- +# Bugs +#----------------------------------------------------------------------------- +bugs " + +=over 4 + +=item + +No negative-cost epsilon cycles are allowed in the transducer. + +=item + +The Viterbi API, the existence of the gfsmviterbi command-line program, +its arguments & optiosn, etc. are HIGHLY UNSTABLE. + +=back + +" + +#----------------------------------------------------------------------------- +# Footer +#----------------------------------------------------------------------------- +#acknowledge `cat acknowledge.pod` + +seealso " +L<gfsmutils> +" diff --git a/gfsm/gfsm/src/programs/gfsmviterbi_cmdparser.c b/gfsm/gfsm/src/programs/gfsmviterbi_cmdparser.c new file mode 100644 index 0000000..f4de7a9 --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmviterbi_cmdparser.c @@ -0,0 +1,473 @@ +/* -*- Mode: C -*- + * + * File: gfsmviterbi_cmdparser.c + * Description: Code for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05 + * generated with the following command: + * /usr/local/bin/optgen.perl -u -l --no-handle-rcfile --nopod -F gfsmviterbi_cmdparser gfsmviterbi.gog + * + * The developers of optgen.perl consider the fixed text that goes in all + * optgen.perl output files to be in the public domain: + * we make no copyright claims on it. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +/* If we use autoconf/autoheader. */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif + +/* Allow user-overrides for PACKAGE and VERSION */ +#ifndef PACKAGE +# define PACKAGE "PACKAGE" +#endif + +#ifndef VERSION +# define VERSION "VERSION" +#endif + + +#ifndef PROGRAM +# define PROGRAM "gfsmviterbi" +#endif + +/* #define cmdline_parser_DEBUG */ + +/* Check for "configure's" getopt check result. */ +#ifndef HAVE_GETOPT_LONG +# include "getopt.h" +#else +# include <getopt.h> +#endif + +#if !defined(HAVE_STRDUP) && !defined(strdup) +# define strdup gengetopt_strdup +#endif /* HAVE_STRDUP */ + +#include "gfsmviterbi_cmdparser.h" + + +/* user code section */ + +/* end user code section */ + + +void +cmdline_parser_print_version (void) +{ + printf("gfsmviterbi (%s %s) by Bryan Jurish <moocow@ling.uni-potsdam.de>\n", PACKAGE, VERSION); +} + +void +cmdline_parser_print_help (void) +{ + cmdline_parser_print_version (); + printf("\n"); + printf("Purpose:\n"); + printf(" EXPERIMENTAL: Compute Viterbi trellis for a linear label sequence in a transducer\n"); + printf("\n"); + + printf("Usage: %s [OPTIONS]... LABELID...\n", "gfsmviterbi"); + + printf("\n"); + printf(" Arguments:\n"); + printf(" LABELID... Input label IDs\n"); + + printf("\n"); + printf(" Options:\n"); + printf(" -h --help Print help and exit.\n"); + printf(" -V --version Print version and exit.\n"); + printf(" -fFSTFILE --fst=FSTFILE Weighted transducer to apply (default=stdin).\n"); + printf(" -zLEVEL --compress=LEVEL Specify compression level of output file.\n"); + printf(" -FFILE --output=FILE Specifiy output file (default=stdout).\n"); +} + +#if !defined(HAVE_STRDUP) && !defined(strdup) +/* gengetopt_strdup(): automatically generated from strdup.c. */ +/* strdup.c replacement of strdup, which is not standard */ +static char * +gengetopt_strdup (const char *s) +{ + char *result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} +#endif /* HAVE_STRDUP */ + +/* clear_args(args_info): clears all args & resets to defaults */ +static void +clear_args(struct gengetopt_args_info *args_info) +{ + args_info->fst_arg = strdup("-"); + args_info->compress_arg = -1; + args_info->output_arg = strdup("-"); +} + + +int +cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) +{ + int c; /* Character of the parsed option. */ + int missing_required_options = 0; + + args_info->help_given = 0; + args_info->version_given = 0; + args_info->fst_given = 0; + args_info->compress_given = 0; + args_info->output_given = 0; + + clear_args(args_info); + + /* rcfile handling */ + + /* end rcfile handling */ + + optarg = 0; + optind = 1; + opterr = 1; + optopt = '?'; + + while (1) + { + int option_index = 0; + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "fst", 1, NULL, 'f' }, + { "compress", 1, NULL, 'z' }, + { "output", 1, NULL, 'F' }, + { NULL, 0, NULL, 0 } + }; + static char short_options[] = { + 'h', + 'V', + 'f', ':', + 'z', ':', + 'F', ':', + '\0' + }; + + c = getopt_long (argc, argv, short_options, long_options, &option_index); + + if (c == -1) break; /* Exit from 'while (1)' loop. */ + + if (cmdline_parser_parse_option(c, long_options[option_index].name, optarg, args_info) != 0) { + exit (EXIT_FAILURE); + } + } /* while */ + + + + if ( missing_required_options ) + exit (EXIT_FAILURE); + + + if (optind < argc) { + int i = 0 ; + args_info->inputs_num = argc - optind ; + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; + while (optind < argc) + args_info->inputs[ i++ ] = strdup (argv[optind++]) ; + } + + return 0; +} + + +/* Parse a single option */ +int +cmdline_parser_parse_option(char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info) +{ + if (!oshort && !(olong && *olong)) return 1; /* ignore null options */ + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "parse_option(): oshort='%c', olong='%s', val='%s'\n", oshort, olong, val);*/ +#endif + + switch (oshort) + { + case 'h': /* Print help and exit. */ + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + break; + + case 'V': /* Print version and exit. */ + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + break; + + case 'f': /* Weighted transducer to apply (default=stdin). */ + if (args_info->fst_given) { + fprintf(stderr, "%s: `--fst' (`-f') option given more than once\n", PROGRAM); + } + args_info->fst_given++; + if (args_info->fst_arg) free(args_info->fst_arg); + args_info->fst_arg = strdup(val); + break; + + case 'z': /* Specify compression level of output file. */ + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + break; + + case 'F': /* Specifiy output file (default=stdout). */ + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + break; + + case 0: /* Long option(s) with no short form */ + /* Print help and exit. */ + if (strcmp(olong, "help") == 0) { + if (args_info->help_given) { + fprintf(stderr, "%s: `--help' (`-h') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_help(); + exit(EXIT_SUCCESS); + + } + + /* Print version and exit. */ + else if (strcmp(olong, "version") == 0) { + if (args_info->version_given) { + fprintf(stderr, "%s: `--version' (`-V') option given more than once\n", PROGRAM); + } + clear_args(args_info); + cmdline_parser_print_version(); + exit(EXIT_SUCCESS); + + } + + /* Weighted transducer to apply (default=stdin). */ + else if (strcmp(olong, "fst") == 0) { + if (args_info->fst_given) { + fprintf(stderr, "%s: `--fst' (`-f') option given more than once\n", PROGRAM); + } + args_info->fst_given++; + if (args_info->fst_arg) free(args_info->fst_arg); + args_info->fst_arg = strdup(val); + } + + /* Specify compression level of output file. */ + else if (strcmp(olong, "compress") == 0) { + if (args_info->compress_given) { + fprintf(stderr, "%s: `--compress' (`-z') option given more than once\n", PROGRAM); + } + args_info->compress_given++; + args_info->compress_arg = (int)atoi(val); + } + + /* Specifiy output file (default=stdout). */ + else if (strcmp(olong, "output") == 0) { + if (args_info->output_given) { + fprintf(stderr, "%s: `--output' (`-F') option given more than once\n", PROGRAM); + } + args_info->output_given++; + if (args_info->output_arg) free(args_info->output_arg); + args_info->output_arg = strdup(val); + } + + else { + fprintf(stderr, "%s: unknown long option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + } + break; + + case '?': /* Invalid Option */ + fprintf(stderr, "%s: unknown option '%s'.\n", PROGRAM, olong); + return (EXIT_FAILURE); + + + default: /* bug: options not considered. */ + fprintf (stderr, "%s: option unknown: %c\n", PROGRAM, oshort); + abort (); + } /* switch */ + return 0; +} + + +/* Initialize options not yet given from environmental defaults */ +void +cmdline_parser_envdefaults(struct gengetopt_args_info *args_info) +{ + + + return; +} + + +/* Load option values from an .rc file */ +void +cmdline_parser_read_rcfile(const char *filename, + struct gengetopt_args_info *args_info, + int user_specified) +{ + char *fullname; + FILE *rcfile; + + if (!filename) return; /* ignore NULL filenames */ + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID) + if (*filename == '~') { + /* tilde-expansion hack */ + struct passwd *pwent = getpwuid(getuid()); + if (!pwent) { + fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid()); + return; + } + if (!pwent->pw_dir) { + fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid()); + return; + } + fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename)); + strcpy(fullname, pwent->pw_dir); + strcat(fullname, filename+1); + } else { + fullname = strdup(filename); + } +#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */ + fullname = strdup(filename); +#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */ + + /* try to open */ + rcfile = fopen(fullname,"r"); + if (!rcfile) { + if (user_specified) { + fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n", + PROGRAM, fullname, strerror(errno)); + } + } + else { + cmdline_parser_read_rc_stream(rcfile, fullname, args_info); + } + + /* cleanup */ + if (fullname != filename) free(fullname); + if (rcfile) fclose(rcfile); + + return; +} + + +/* Parse option values from an .rc file : guts */ +#define OPTPARSE_GET 32 +void +cmdline_parser_read_rc_stream(FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info) +{ + char *optname = (char *)malloc(OPTPARSE_GET); + char *optval = (char *)malloc(OPTPARSE_GET); + size_t onsize = OPTPARSE_GET; + size_t ovsize = OPTPARSE_GET; + size_t onlen = 0; + size_t ovlen = 0; + int lineno = 0; + char c; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'):\n", filename); +#endif + + while ((c = fgetc(rcfile)) != EOF) { + onlen = 0; + ovlen = 0; + lineno++; + + /* -- get next option-name */ + /* skip leading space and comments */ + if (isspace(c)) continue; + if (c == '#') { + while ((c = fgetc(rcfile)) != EOF) { + if (c == '\n') break; + } + continue; + } + + /* parse option-name */ + while (c != EOF && c != '=' && !isspace(c)) { + /* re-allocate if necessary */ + if (onlen >= onsize-1) { + char *tmp = (char *)malloc(onsize+OPTPARSE_GET); + strcpy(tmp,optname); + free(optname); + + onsize += OPTPARSE_GET; + optname = tmp; + } + optname[onlen++] = c; + c = fgetc(rcfile); + } + optname[onlen++] = '\0'; + +#ifdef cmdline_parser_DEBUG + fprintf(stderr, "cmdline_parser_read_rc_stream('%s'): line %d: optname='%s'\n", + filename, lineno, optname); +#endif + + /* -- get next option-value */ + /* skip leading space */ + while ((c = fgetc(rcfile)) != EOF && isspace(c)) { + ; + } + + /* parse option-value */ + while (c != EOF && c != '\n') { + /* re-allocate if necessary */ + if (ovlen >= ovsize-1) { + char *tmp = (char *)malloc(ovsize+OPTPARSE_GET); + strcpy(tmp,optval); + free(optval); + ovsize += OPTPARSE_GET; + optval = tmp; + } + optval[ovlen++] = c; + c = fgetc(rcfile); + } + optval[ovlen++] = '\0'; + + /* now do the action for the option */ + if (cmdline_parser_parse_option('\0',optname,optval,args_info) != 0) { + fprintf(stderr, "%s: error in file '%s' at line %d.\n", PROGRAM, filename, lineno); + + } + } + + /* cleanup */ + free(optname); + free(optval); + + return; +} diff --git a/gfsm/gfsm/src/programs/gfsmviterbi_cmdparser.h b/gfsm/gfsm/src/programs/gfsmviterbi_cmdparser.h new file mode 100644 index 0000000..d38087d --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmviterbi_cmdparser.h @@ -0,0 +1,65 @@ +/* -*- Mode: C -*- + * + * File: gfsmviterbi_cmdparser.h + * Description: Headers for command-line parser struct gengetopt_args_info. + * + * File autogenerated by optgen.perl version 0.05. + * + */ + +#ifndef gfsmviterbi_cmdparser_h +#define gfsmviterbi_cmdparser_h + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * moocow: Never set PACKAGE and VERSION here. + */ + +struct gengetopt_args_info { + char * fst_arg; /* Weighted transducer to apply (default=stdin). (default=-). */ + int compress_arg; /* Specify compression level of output file. (default=-1). */ + char * output_arg; /* Specifiy output file (default=stdout). (default=-). */ + + int help_given; /* Whether help was given */ + int version_given; /* Whether version was given */ + int fst_given; /* Whether fst was given */ + int compress_given; /* Whether compress was given */ + int output_given; /* Whether output was given */ + + char **inputs; /* unnamed arguments */ + unsigned inputs_num; /* number of unnamed arguments */ +}; + +/* read rc files (if any) and parse all command-line options in one swell foop */ +int cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info); + +/* instantiate defaults from environment variables: you must call this yourself! */ +void cmdline_parser_envdefaults (struct gengetopt_args_info *args_info); + +/* read a single rc-file */ +void cmdline_parser_read_rcfile (const char *filename, + struct gengetopt_args_info *args_info, + int user_specified); + +/* read a single rc-file (stream) */ +void cmdline_parser_read_rc_stream (FILE *rcfile, + const char *filename, + struct gengetopt_args_info *args_info); + +/* parse a single option */ +int cmdline_parser_parse_option (char oshort, const char *olong, const char *val, + struct gengetopt_args_info *args_info); + +/* print help message */ +void cmdline_parser_print_help(void); + +/* print version */ +void cmdline_parser_print_version(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* gfsmviterbi_cmdparser_h */ diff --git a/gfsm/gfsm/src/programs/gfsmviterbi_main.c b/gfsm/gfsm/src/programs/gfsmviterbi_main.c new file mode 100644 index 0000000..33bb01a --- /dev/null +++ b/gfsm/gfsm/src/programs/gfsmviterbi_main.c @@ -0,0 +1,125 @@ +/* + gfsm-utils : finite state automaton utilities + Copyright (C) 2005 by Bryan Jurish <moocow@ling.uni-potsdam.de> + + 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 +*/ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> + +#include <gfsm.h> + +#include "gfsmviterbi_cmdparser.h" + +/*-------------------------------------------------------------------------- + * Globals + *--------------------------------------------------------------------------*/ +char *progname = "gfsmviterbi"; + +//-- options +struct gengetopt_args_info args; + +//-- files +const char *fstfilename = "-"; +const char *outfilename = "-"; + +//-- global structs +gfsmAutomaton *fst = NULL; +gfsmError *err = NULL; + +/*-------------------------------------------------------------------------- + * Option Processing + *--------------------------------------------------------------------------*/ +void get_my_options(int argc, char **argv) +{ + if (cmdline_parser(argc, argv, &args) != 0) + exit(1); + + //-- load environmental defaults + //cmdline_parser_envdefaults(&args); + + //-- filenames + if (args.fst_given) fstfilename = args.fst_arg; + outfilename = args.output_arg; + + //-- load FST + fst = gfsm_automaton_new(); + if (!gfsm_automaton_load_bin_filename(fst, fstfilename, &err)) { + g_printerr("%s: load failed for FST file '%s': %s\n", progname, fstfilename, err->message); + exit(255); + } +} + +/*-------------------------------------------------------------------------- + * viterbi_labels() + */ +gfsmAutomaton *viterbi_labels(gfsmAutomaton *fst, int argc, char **argv) +{ + gfsmLabelVector *vec = g_ptr_array_sized_new(argc); + char *s=NULL, *tail=NULL; + gfsmLabelVal lab; + gfsmAutomaton *trellis = NULL; + int i; + + //-- fill input vector + for (i=0; i < argc; i++) { + for (s=argv[i], lab=strtol(s,&tail,0); s != tail; s=tail, lab=strtol(s,&tail,0)) { + g_ptr_array_add(vec, (gpointer)lab); + } + } + + //-- actual viterbi lookup + trellis = gfsm_automaton_lookup_viterbi(fst, vec, trellis); + + //-- cleanup + g_ptr_array_free(vec,TRUE); + + return trellis; +} + +/*-------------------------------------------------------------------------- + * MAIN + *--------------------------------------------------------------------------*/ +int main (int argc, char **argv) +{ + gfsmAutomaton *trellis; + + GFSM_INIT + get_my_options(argc,argv); + + //-- process input + trellis = viterbi_labels(fst, args.inputs_num, args.inputs); + + //-- save output + if (!gfsm_automaton_save_bin_filename(trellis,outfilename,args.compress_arg,&err)) { + g_printerr("%s: store failed to '%s': %s\n", progname, outfilename, err->message); + exit(4); + } + + //-- cleanup + if (fst) gfsm_automaton_free(fst); + if (trellis) gfsm_automaton_free(trellis); + + GFSM_FINISH + return 0; +} diff --git a/gfsm/gfsm/src/programs/isect1.tfst b/gfsm/gfsm/src/programs/isect1.tfst new file mode 100644 index 0000000..5781fec --- /dev/null +++ b/gfsm/gfsm/src/programs/isect1.tfst @@ -0,0 +1,4 @@ +0 1 0 0 +1 +1 2 1 1 +2 diff --git a/gfsm/gfsm/src/programs/isect2.tfst b/gfsm/gfsm/src/programs/isect2.tfst new file mode 100644 index 0000000..d64b6cb --- /dev/null +++ b/gfsm/gfsm/src/programs/isect2.tfst @@ -0,0 +1,5 @@ +0 0 1 1 +0 1 0 0 +0 +1 1 2 2 +1 diff --git a/gfsm/gfsm/src/programs/lkptest.tfst b/gfsm/gfsm/src/programs/lkptest.tfst new file mode 100644 index 0000000..40c6d7a --- /dev/null +++ b/gfsm/gfsm/src/programs/lkptest.tfst @@ -0,0 +1,13 @@ +0 0 1 2 1 +0 0 2 3 1 +0 0 3 1 1 +0 0 + +0 1 2 2 1 +1 2 2 2 1 +2 3 3 3 1 +3 0 + +0 4 1 0 1 +4 5 0 3 1 +5 0 diff --git a/gfsm/gfsm/src/programs/p1.tfst b/gfsm/gfsm/src/programs/p1.tfst new file mode 100644 index 0000000..671e9e0 --- /dev/null +++ b/gfsm/gfsm/src/programs/p1.tfst @@ -0,0 +1,5 @@ +0 1 1 1 +0 1 2 2 +1 +1 2 3 3 +2 diff --git a/gfsm/gfsm/src/programs/p2.tfst b/gfsm/gfsm/src/programs/p2.tfst new file mode 100644 index 0000000..f8fd663 --- /dev/null +++ b/gfsm/gfsm/src/programs/p2.tfst @@ -0,0 +1,3 @@ +0 1 4 4 +0 1 5 5 +1 diff --git a/gfsm/gfsm/src/programs/prunetest.tfst b/gfsm/gfsm/src/programs/prunetest.tfst new file mode 100644 index 0000000..0dc8950 --- /dev/null +++ b/gfsm/gfsm/src/programs/prunetest.tfst @@ -0,0 +1,6 @@ +0 1 1 1 0 +1 2 2 2 0 +0 3 3 3 0 +1 0 +4 5 4 4 0 +5 0 diff --git a/gfsm/gfsm/src/programs/r1.tfst b/gfsm/gfsm/src/programs/r1.tfst new file mode 100644 index 0000000..f9608f2 --- /dev/null +++ b/gfsm/gfsm/src/programs/r1.tfst @@ -0,0 +1,9 @@ +0 1 1 1 +0 2 0 1 +0 3 1 0 +1 4 4 4 +2 5 5 5 +3 6 6 6 +4 +5 +6 diff --git a/gfsm/gfsm/src/programs/r2.tfst b/gfsm/gfsm/src/programs/r2.tfst new file mode 100644 index 0000000..a61738d --- /dev/null +++ b/gfsm/gfsm/src/programs/r2.tfst @@ -0,0 +1,4 @@ +0 1 24 7 +0 2 7 24 +1 10 +2 20 diff --git a/gfsm/gfsm/src/programs/renumber.tfst b/gfsm/gfsm/src/programs/renumber.tfst new file mode 100644 index 0000000..b4697b0 --- /dev/null +++ b/gfsm/gfsm/src/programs/renumber.tfst @@ -0,0 +1,5 @@ +5 4 43 0 0 +5 1 42 0 0 +7 0 +1 7 1 1 0 +4 7 1 1 0 diff --git a/gfsm/gfsm/src/programs/renumber2.tfst b/gfsm/gfsm/src/programs/renumber2.tfst new file mode 100644 index 0000000..1c71c3d --- /dev/null +++ b/gfsm/gfsm/src/programs/renumber2.tfst @@ -0,0 +1,8 @@ +5 1 42 0 0 +5 4 43 0 0 +6 7 1 1 0 +7 0 +0 1 42 0 0 +1 7 1 1 0 +3 4 43 0 0 +4 7 1 1 0 diff --git a/gfsm/gfsm/src/programs/rev1.tfst b/gfsm/gfsm/src/programs/rev1.tfst new file mode 100644 index 0000000..5a428b4 --- /dev/null +++ b/gfsm/gfsm/src/programs/rev1.tfst @@ -0,0 +1,7 @@ +0 1 0 1 1 +1 2 1 2 12 +2 2 2 2 22 +2 20 +1 3 1 3 13 +3 4 3 4 34 +4 40
\ No newline at end of file diff --git a/gfsm/gfsm/src/programs/rme.tfst b/gfsm/gfsm/src/programs/rme.tfst new file mode 100644 index 0000000..06922fa --- /dev/null +++ b/gfsm/gfsm/src/programs/rme.tfst @@ -0,0 +1,6 @@ +0 0 0 0 0 +0 1 0 0 1 +1 2 0 0 2 +0 2 0 0 10 +2 3 1 1 3 +3 diff --git a/gfsm/gfsm/src/programs/rme2.tfst b/gfsm/gfsm/src/programs/rme2.tfst new file mode 100644 index 0000000..b30d66e --- /dev/null +++ b/gfsm/gfsm/src/programs/rme2.tfst @@ -0,0 +1,8 @@ +5 0 0 0 0 +5 3 0 0 0 +6 7 0 0 0 +7 0 +0 1 42 0 0 +1 6 0 0 0 +3 4 43 0 0 +4 6 0 0 0 diff --git a/gfsm/gfsm/src/programs/rme2b.tfst b/gfsm/gfsm/src/programs/rme2b.tfst new file mode 100644 index 0000000..da2ffd2 --- /dev/null +++ b/gfsm/gfsm/src/programs/rme2b.tfst @@ -0,0 +1,8 @@ +5 0 0 0 0 +5 3 0 0 0 +6 7 1 1 0 +7 0 +0 1 42 0 0 +1 6 0 0 0 +3 4 43 0 0 +4 6 0 0 0 diff --git a/gfsm/gfsm/src/programs/rme3.tfst b/gfsm/gfsm/src/programs/rme3.tfst new file mode 100644 index 0000000..8235e2b --- /dev/null +++ b/gfsm/gfsm/src/programs/rme3.tfst @@ -0,0 +1,5 @@ +0 1 42 0 +0 2 43 0 +1 3 0 0 0 +2 3 0 0 0 +3 0 diff --git a/gfsm/gfsm/src/programs/sort.tfst b/gfsm/gfsm/src/programs/sort.tfst new file mode 100644 index 0000000..a5bb1c7 --- /dev/null +++ b/gfsm/gfsm/src/programs/sort.tfst @@ -0,0 +1,7 @@ +0 1 1 2 1 +0 1 2 1 2 +0 1 2 2 3 +0 1 1 2 3 +0 1 2 1 2 +0 1 2 2 1 +1 diff --git a/gfsm/gfsm/src/programs/sortme.tfst b/gfsm/gfsm/src/programs/sortme.tfst new file mode 100644 index 0000000..d48ea71 --- /dev/null +++ b/gfsm/gfsm/src/programs/sortme.tfst @@ -0,0 +1,15 @@ +0 1 1 10 10.0 +0 1 2 10 10.0 +0 1 1 20 10.0 +0 1 2 20 10.0 +0 1 3 10 10.0 +0 1 3 20 10.0 +0 1 3 30 10.0 +0 1 4 40 1.0 +0 1 4 40 2.0 +0 1 4 40 3.0 +0 0 5 50 10.0 +0 0 5 50 10.0 +0 0 5 50 10.0 +0 0.0 +1 0.0 diff --git a/gfsm/gfsm/src/programs/symtest.lab b/gfsm/gfsm/src/programs/symtest.lab new file mode 100644 index 0000000..4d677d5 --- /dev/null +++ b/gfsm/gfsm/src/programs/symtest.lab @@ -0,0 +1,15 @@ +<epsilon> 0 +a 1 +b 2 +c 3 +A 4 +B 5 +C 6 +acc 7 +dat 8 +sg 9 +pl 10 +_NOUN 11 +_Case 12 +_Number 13 +_VERB 14 diff --git a/gfsm/gfsm/src/programs/symtest.sym b/gfsm/gfsm/src/programs/symtest.sym new file mode 100644 index 0000000..f151932 --- /dev/null +++ b/gfsm/gfsm/src/programs/symtest.sym @@ -0,0 +1,8 @@ +Lower a b c +Upper A B C + +Case acc dat +Number sg pl + +Category: NOUN Case Number +Category: VERB Number diff --git a/gfsm/gfsm/src/programs/test+lab.tfst b/gfsm/gfsm/src/programs/test+lab.tfst new file mode 100644 index 0000000..252d6a6 --- /dev/null +++ b/gfsm/gfsm/src/programs/test+lab.tfst @@ -0,0 +1,3 @@ +0 1 a b 0 +1 2 c d 0 +2 0 diff --git a/gfsm/gfsm/src/programs/test-rn.tfst b/gfsm/gfsm/src/programs/test-rn.tfst new file mode 100644 index 0000000..694ddcd --- /dev/null +++ b/gfsm/gfsm/src/programs/test-rn.tfst @@ -0,0 +1,8 @@ +0 1 1 1 0 +0 0 +1 0 +2 0 +3 0 +4 +5 +6 diff --git a/gfsm/gfsm/src/programs/test.lab b/gfsm/gfsm/src/programs/test.lab new file mode 100644 index 0000000..b7c05ae --- /dev/null +++ b/gfsm/gfsm/src/programs/test.lab @@ -0,0 +1,15 @@ +<eps> 0 +a 1 +b 2 +c 3 +d 4 +e 5 +f 6 +fourtwenty 420 +twentyfour 24 +seven 7 +( 10 +) 11 +FOO 12 +BAR 13 +BAZ 14 diff --git a/gfsm/gfsm/src/programs/test.tfst b/gfsm/gfsm/src/programs/test.tfst new file mode 100644 index 0000000..02acee3 --- /dev/null +++ b/gfsm/gfsm/src/programs/test.tfst @@ -0,0 +1,3 @@ +0 1 420 24 0.7 +1 2 0 0 99 +2 diff --git a/gfsm/gfsm/src/programs/test2.tfst b/gfsm/gfsm/src/programs/test2.tfst new file mode 100644 index 0000000..37fe96a --- /dev/null +++ b/gfsm/gfsm/src/programs/test2.tfst @@ -0,0 +1,7 @@ +0 1 1 3 .042 +0 2 2 2 .24 +0 3 3 1 .07 +1 +2 +3 + diff --git a/gfsm/gfsm/src/programs/test3.tfst b/gfsm/gfsm/src/programs/test3.tfst new file mode 100644 index 0000000..669bfc9 --- /dev/null +++ b/gfsm/gfsm/src/programs/test3.tfst @@ -0,0 +1,6 @@ +0 1 1 1 100 +0 4 4 4 400 +1 2 2 2 200 +2 3 3 3 300 +3 0 +4 0 diff --git a/gfsm/gfsm/src/programs/test4.tfst b/gfsm/gfsm/src/programs/test4.tfst new file mode 100644 index 0000000..44d5c9e --- /dev/null +++ b/gfsm/gfsm/src/programs/test4.tfst @@ -0,0 +1,9 @@ +0 1 0 0 0 +0 1 0 0 1 +0 1 0 1 0 +0 1 0 1 1 +0 1 1 0 0 +0 1 1 0 1 +0 1 1 1 0 +0 1 1 1 1 +1 diff --git a/gfsm/gfsm/src/programs/test4b.tfst b/gfsm/gfsm/src/programs/test4b.tfst new file mode 100644 index 0000000..8890ba6 --- /dev/null +++ b/gfsm/gfsm/src/programs/test4b.tfst @@ -0,0 +1,9 @@ +0 1 1 1 1 +0 1 1 1 0 +0 1 1 0 1 +0 1 1 0 0 +0 1 0 1 1 +0 1 0 1 0 +0 1 0 0 1 +0 1 0 0 0 +1
\ No newline at end of file diff --git a/gfsm/gfsm/src/programs/test5.tfst b/gfsm/gfsm/src/programs/test5.tfst new file mode 100644 index 0000000..ac8c248 --- /dev/null +++ b/gfsm/gfsm/src/programs/test5.tfst @@ -0,0 +1,3 @@ +0 1 1 1 0 +1 2 2 2 0 +2 0 diff --git a/gfsm/gfsm/src/programs/test6.tfst b/gfsm/gfsm/src/programs/test6.tfst new file mode 100644 index 0000000..4c306da --- /dev/null +++ b/gfsm/gfsm/src/programs/test6.tfst @@ -0,0 +1,12 @@ +0 1 0 0 0 +0 2 0 0 0 +0 3 0 0 0 +3 4 0 0 0 +1 10 1 1 0 +2 20 2 2 0 +4 5 4 4 0 +4 1 4 4 1 +10 +20 +5 + diff --git a/gfsm/gfsm/src/programs/test7.lab b/gfsm/gfsm/src/programs/test7.lab new file mode 100644 index 0000000..0cedd17 --- /dev/null +++ b/gfsm/gfsm/src/programs/test7.lab @@ -0,0 +1,4 @@ +<eps> 0 +a 1 +b 2 +c 3 diff --git a/gfsm/gfsm/src/programs/test7.tfst b/gfsm/gfsm/src/programs/test7.tfst new file mode 100644 index 0000000..c96f7e6 --- /dev/null +++ b/gfsm/gfsm/src/programs/test7.tfst @@ -0,0 +1,2 @@ +0 1 1 1 0 +1 0 diff --git a/gfsm/gfsm/src/programs/test8.tfst b/gfsm/gfsm/src/programs/test8.tfst new file mode 100644 index 0000000..9009985 --- /dev/null +++ b/gfsm/gfsm/src/programs/test8.tfst @@ -0,0 +1,4 @@ +0 1 1 1 0 +0 2 2 2 0 +1 0 +2 0 diff --git a/gfsm/gfsm/src/programs/test9.tfst b/gfsm/gfsm/src/programs/test9.tfst new file mode 100644 index 0000000..47867d2 --- /dev/null +++ b/gfsm/gfsm/src/programs/test9.tfst @@ -0,0 +1,4 @@ +0 0 +1 0 +2 0 +3 0 diff --git a/gfsm/gfsm/src/programs/testa.tfsa b/gfsm/gfsm/src/programs/testa.tfsa new file mode 100644 index 0000000..f2926c5 --- /dev/null +++ b/gfsm/gfsm/src/programs/testa.tfsa @@ -0,0 +1,6 @@ +0 0 0 +0 1 1 10 +0 +1 2 2 20 +1 100 +2 200 diff --git a/gfsm/gfsm/src/programs/testa.tfst b/gfsm/gfsm/src/programs/testa.tfst new file mode 100644 index 0000000..1078be4 --- /dev/null +++ b/gfsm/gfsm/src/programs/testa.tfst @@ -0,0 +1,6 @@ +0 0 0 +0 1 1 1 +0 +1 2 2 2 20 +1 100 +2 200 diff --git a/gfsm/gfsm/src/programs/testrn.tfst b/gfsm/gfsm/src/programs/testrn.tfst new file mode 100644 index 0000000..0bcf201 --- /dev/null +++ b/gfsm/gfsm/src/programs/testrn.tfst @@ -0,0 +1,3 @@ +300 100 1 1 +100 200 2 2 +200 0 diff --git a/gfsm/gfsm/src/programs/vit-in.lab b/gfsm/gfsm/src/programs/vit-in.lab new file mode 100644 index 0000000..5c74e25 --- /dev/null +++ b/gfsm/gfsm/src/programs/vit-in.lab @@ -0,0 +1,3 @@ +<eps> 0 +a 1 +b 2 diff --git a/gfsm/gfsm/src/programs/vit-out.lab b/gfsm/gfsm/src/programs/vit-out.lab new file mode 100644 index 0000000..bd1493c --- /dev/null +++ b/gfsm/gfsm/src/programs/vit-out.lab @@ -0,0 +1,7 @@ +<eps> 0 +a_A 1 +a_B 2 +b_A 3 +b_B 4 +<eps>_A 5 +<eps>_B 6 diff --git a/gfsm/gfsm/src/programs/vit-q.lab b/gfsm/gfsm/src/programs/vit-q.lab new file mode 100644 index 0000000..0fbd83b --- /dev/null +++ b/gfsm/gfsm/src/programs/vit-q.lab @@ -0,0 +1,3 @@ +<BOS> 0 +A 1 +B 2 diff --git a/gfsm/gfsm/src/programs/vit.tfst b/gfsm/gfsm/src/programs/vit.tfst new file mode 100644 index 0000000..e92ec55 --- /dev/null +++ b/gfsm/gfsm/src/programs/vit.tfst @@ -0,0 +1,17 @@ +<BOS> A a a_A 1 +<BOS> A b b_A 1 +<BOS> B a a_B 1 +<BOS> B b b_B 1 +<BOS> 1 + +A A a a_A .4 +A A b b_A .3 +A B a a_B .2 +A B b b_B .1 +A 1 + +B B b b_B .4 +B A a a_A .3 +B A b b_A .2 +B B a a_B .1 +B 1 diff --git a/gfsm/gfsm/src/programs/vit2.tfst b/gfsm/gfsm/src/programs/vit2.tfst new file mode 100644 index 0000000..879b3a1 --- /dev/null +++ b/gfsm/gfsm/src/programs/vit2.tfst @@ -0,0 +1,15 @@ +<BOS> A <eps> <eps>_A .4 +<BOS> A a a_A .3 +<BOS> B <eps> <eps>_B .2 +<BOS> B b b_B .1 +<BOS> 1 + +A A a a_A .7 +A B <eps> <eps>_B .2 +A B b b_B .1 +A 1 + +B B b b_B .7 +B A a a_A .2 +B A <eps> <eps>_A .1 +B 1 |