\doxysection{main.\+c}
\label{main_8c_source}\index{src/sphinx\_jsgf2fsg/main.c@{src/sphinx\_jsgf2fsg/main.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2007\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00039\ }
\DoxyCodeLine{00040\ \textcolor{preprocessor}{\#include\ <sphinxbase/hash\_table.h>}}
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#include\ <sphinxbase/fsg\_model.h>}}
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ <sphinxbase/jsgf.h>}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ <sphinxbase/err.h>}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ <sphinxbase/strfuncs.h>}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \textcolor{keyword}{static}\ \textcolor{keyword}{const}\ arg\_t\ defn[]\ =\ \{}
\DoxyCodeLine{00047\ \ \ \{\ \textcolor{stringliteral}{"{}-\/help"{}},}
\DoxyCodeLine{00048\ \ \ \ \ ARG\_BOOLEAN,}
\DoxyCodeLine{00049\ \ \ \ \ \textcolor{stringliteral}{"{}no"{}},}
\DoxyCodeLine{00050\ \ \ \ \ \textcolor{stringliteral}{"{}Shows\ the\ usage\ of\ the\ tool"{}}\},}
\DoxyCodeLine{00051\ }
\DoxyCodeLine{00052\ \ \ \{\ \textcolor{stringliteral}{"{}-\/jsgf"{}},}
\DoxyCodeLine{00053\ \ \ \ \ REQARG\_STRING,}
\DoxyCodeLine{00054\ \ \ \ \ NULL,}
\DoxyCodeLine{00055\ \ \ \ \ \textcolor{stringliteral}{"{}Input\ grammar\ in\ jsgf\ format\ (required)"{}}\},}
\DoxyCodeLine{00056\ }
\DoxyCodeLine{00057\ \ \ \{\ \textcolor{stringliteral}{"{}-\/rule"{}},}
\DoxyCodeLine{00058\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00059\ \ \ \ \ NULL,}
\DoxyCodeLine{00060\ \ \ \ \ \textcolor{stringliteral}{"{}Root\ rule\ name\ (optional)"{}}\},}
\DoxyCodeLine{00061\ }
\DoxyCodeLine{00062\ \ \ \{\ \textcolor{stringliteral}{"{}-\/fsg"{}},}
\DoxyCodeLine{00063\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00064\ \ \ \ \ NULL,}
\DoxyCodeLine{00065\ \ \ \ \ \textcolor{stringliteral}{"{}Output\ grammar\ in\ fsg\ format"{}}\},}
\DoxyCodeLine{00066\ }
\DoxyCodeLine{00067\ \ \ \{\ \textcolor{stringliteral}{"{}-\/fsm"{}},}
\DoxyCodeLine{00068\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00069\ \ \ \ \ NULL,}
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{stringliteral}{"{}Output\ grammar\ in\ FSM\ format"{}}\},}
\DoxyCodeLine{00071\ }
\DoxyCodeLine{00072\ \ \ \{\ \textcolor{stringliteral}{"{}-\/symtab"{}},}
\DoxyCodeLine{00073\ \ \ \ \ ARG\_STRING,}
\DoxyCodeLine{00074\ \ \ \ \ NULL,}
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{stringliteral}{"{}Output\ symtab\ for\ grammar\ in\ FSM\ format"{}}\},}
\DoxyCodeLine{00076\ }
\DoxyCodeLine{00077\ \ \ \{\ \textcolor{stringliteral}{"{}-\/compile"{}},}
\DoxyCodeLine{00078\ \ \ \ \ ARG\_BOOLEAN,}
\DoxyCodeLine{00079\ \ \ \ \ \textcolor{stringliteral}{"{}no"{}},}
\DoxyCodeLine{00080\ \ \ \ \ \textcolor{stringliteral}{"{}Compute\ grammar\ closure\ to\ speedup\ loading"{}}\},}
\DoxyCodeLine{00081\ }
\DoxyCodeLine{00082\ \ \ \{\ NULL,\ 0,\ NULL,\ NULL\ \}}
\DoxyCodeLine{00083\ \};}
\DoxyCodeLine{00084\ }
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00087\ usagemsg(\textcolor{keywordtype}{char}\ *pgm)}
\DoxyCodeLine{00088\ \{}
\DoxyCodeLine{00089\ \ \ \ \ E\_INFO(\textcolor{stringliteral}{"{}Usage:\ \%s\ -\/jsgf\ <input.jsgf>\ -\/rule\ <rule\ name>\(\backslash\)\(\backslash\)\(\backslash\)n"{}},\ pgm);}
\DoxyCodeLine{00090\ \ \ \ \ E\_INFOCONT(\textcolor{stringliteral}{"{}\(\backslash\)t[-\/fsm\ yes/no]\ [-\/compile\ yes/no]\(\backslash\)n"{}});}
\DoxyCodeLine{00091\ \ \ \ \ E\_INFOCONT(\textcolor{stringliteral}{"{}\(\backslash\)t-\/fsg\ <output.fsg>\(\backslash\)n"{}});}
\DoxyCodeLine{00092\ }
\DoxyCodeLine{00093\ \ \ \ \ exit(0);}
\DoxyCodeLine{00094\ \}}
\DoxyCodeLine{00095\ }
\DoxyCodeLine{00096\ \textcolor{keyword}{static}\ fsg\_model\_t\ *}
\DoxyCodeLine{00097\ get\_fsg(jsgf\_t\ *grammar,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *name)}
\DoxyCodeLine{00098\ \{}
\DoxyCodeLine{00099\ \ \ \ \ jsgf\_rule\_iter\_t\ *itor;}
\DoxyCodeLine{00100\ \ \ \ \ logmath\_t\ *lmath\ =\ logmath\_init(1.0001,\ 0,\ 0);}
\DoxyCodeLine{00101\ \ \ \ \ fsg\_model\_t\ *fsg\ =\ NULL;}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ \textcolor{keywordflow}{for}\ (itor\ =\ jsgf\_rule\_iter(grammar);\ itor;}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \ itor\ =\ jsgf\_rule\_iter\_next(itor))\ \{}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ jsgf\_rule\_t\ *rule\ =\ jsgf\_rule\_iter\_rule(itor);}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ \textcolor{keyword}{const}\ *rule\_name\ =\ jsgf\_rule\_name(rule);}
\DoxyCodeLine{00107\ }
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((name\ ==\ NULL\ \&\&\ jsgf\_rule\_public(rule))}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ \ \ \ \ ||\ (name\ \&\&\ strlen(rule\_name)-\/2\ ==\ strlen(name)\ \&\&}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 0\ ==\ strncmp(rule\_name\ +\ 1,\ name,\ strlen(rule\_name)\ -\/\ 2)))\ \{}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\ =\ jsgf\_build\_fsg\_raw(grammar,\ rule,\ logmath\_retain(lmath),\ 1.0);}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \ \ \ \ jsgf\_rule\_iter\_free(itor);}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00115\ \ \ \ \ \}}
\DoxyCodeLine{00116\ }
\DoxyCodeLine{00117\ \ \ \ \ logmath\_free(lmath);}
\DoxyCodeLine{00118\ \ \ \ \ \textcolor{keywordflow}{return}\ fsg;}
\DoxyCodeLine{00119\ \}}
\DoxyCodeLine{00120\ }
\DoxyCodeLine{00121\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00122\ main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}\ *argv[])}
\DoxyCodeLine{00123\ \{}
\DoxyCodeLine{00124\ \ \ \ \ jsgf\_t\ *jsgf;}
\DoxyCodeLine{00125\ \ \ \ \ fsg\_model\_t\ *fsg;}
\DoxyCodeLine{00126\ \ \ \ \ cmd\_ln\_t\ *config;}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00128\ \ \ \ \ \textcolor{keywordflow}{if}\ ((config\ =\ cmd\_ln\_parse\_r(NULL,\ defn,\ argc,\ argv,\ TRUE))\ ==\ NULL)}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00131\ \ \ \ \ \textcolor{keywordflow}{if}\ (cmd\_ln\_boolean\_r(config,\ \textcolor{stringliteral}{"{}-\/help"{}}))\ \{}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ usagemsg(argv[0]);}
\DoxyCodeLine{00133\ \ \ \ \ \}}
\DoxyCodeLine{00134\ }
\DoxyCodeLine{00135\ \ \ \ \ jsgf\ =\ jsgf\_parse\_file(cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/jsgf"{}}),\ NULL);}
\DoxyCodeLine{00136\ \ \ \ \ \textcolor{keywordflow}{if}\ (jsgf\ ==\ NULL)\ \{}
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00138\ \ \ \ \ \}}
\DoxyCodeLine{00139\ }
\DoxyCodeLine{00140\ \ \ \ \ fsg\ =\ get\_fsg(jsgf,\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/rule"{}})\ ?\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/rule"{}})\ :\ NULL);}
\DoxyCodeLine{00141\ \ \ \ \ }
\DoxyCodeLine{00142\ \ \ \ \ \textcolor{keywordflow}{if}\ (cmd\_ln\_boolean\_r(config,\ \textcolor{stringliteral}{"{}-\/compile"{}}))\ \{}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ fsg\_model\_null\_trans\_closure(fsg,\ NULL);}
\DoxyCodeLine{00144\ \ \ \ \ \}}
\DoxyCodeLine{00145\ }
\DoxyCodeLine{00146\ \ \ \ \ }
\DoxyCodeLine{00147\ \ \ \ \ \textcolor{keywordflow}{if}\ (cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/fsm"{}}))\ \{}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}*\ outfile\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/fsm"{}});}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}*\ symfile\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/symtab"{}});}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (outfile)}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_writefile\_fsm(fsg,\ outfile);}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_write\_fsm(fsg,\ stdout);}
\DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (symfile)}
\DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_writefile\_symtab(fsg,\ symfile);}
\DoxyCodeLine{00156\ \ \ \ \ \}}
\DoxyCodeLine{00157\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00158\ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *outfile\ =\ cmd\_ln\_str\_r(config,\ \textcolor{stringliteral}{"{}-\/fsg"{}});}
\DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (outfile)}
\DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_writefile(fsg,\ outfile);}
\DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \ \ \ \ fsg\_model\_write(fsg,\ stdout);}
\DoxyCodeLine{00163\ \ \ \ \ \}}
\DoxyCodeLine{00164\ \ \ \ \ fsg\_model\_free(fsg);}
\DoxyCodeLine{00165\ \ \ \ \ jsgf\_grammar\_free(jsgf);}
\DoxyCodeLine{00166\ }
\DoxyCodeLine{00167\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00168\ \}}
\DoxyCodeLine{00169\ }
\DoxyCodeLine{00170\ }
\DoxyCodeLine{00171\ \textcolor{preprocessor}{\#if\ defined(\_WIN32\_WCE)}}
\DoxyCodeLine{00172\ \textcolor{preprocessor}{\#pragma\ comment(linker,"{}/entry:mainWCRTStartup"{})}}
\DoxyCodeLine{00173\ \textcolor{preprocessor}{\#include\ <windows.h>}}
\DoxyCodeLine{00174\ }
\DoxyCodeLine{00175\ \textcolor{comment}{//Windows\ Mobile\ has\ the\ Unicode\ main\ only}}
\DoxyCodeLine{00176\ \textcolor{keywordtype}{int}\ wmain(int32\ argc,\ \textcolor{keywordtype}{wchar\_t}\ *wargv[])\ \{}
\DoxyCodeLine{00177\ \ \ \ \ \textcolor{keywordtype}{char}**\ argv;}
\DoxyCodeLine{00178\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ wlen;}
\DoxyCodeLine{00179\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ len;}
\DoxyCodeLine{00180\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00181\ }
\DoxyCodeLine{00182\ \ \ \ \ argv\ =\ malloc(argc*\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char}*));}
\DoxyCodeLine{00183\ \ \ \ \ \textcolor{keywordflow}{for}\ (i=0;\ i<argc;\ i++)\{}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ wlen\ =\ lstrlenW(wargv[i]);}
\DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ len\ =\ wcstombs(NULL,\ wargv[i],\ wlen);}
\DoxyCodeLine{00186\ \ \ \ \ \ \ \ \ argv[i]\ =\ malloc(len+1);}
\DoxyCodeLine{00187\ \ \ \ \ \ \ \ \ wcstombs(argv[i],\ wargv[i],\ wlen);}
\DoxyCodeLine{00188\ \ \ \ \ \}}
\DoxyCodeLine{00189\ }
\DoxyCodeLine{00190\ \ \ \ \ \textcolor{comment}{//assuming\ ASCII\ parameters}}
\DoxyCodeLine{00191\ \ \ \ \ \textcolor{keywordflow}{return}\ main(argc,\ argv);}
\DoxyCodeLine{00192\ \}}
\DoxyCodeLine{00193\ \textcolor{preprocessor}{\#endif}}

\end{DoxyCode}
