\input cwebmac

\input csmac % Makra pro �e�tinu
\pageheight=9.5in \fullpageheight=9.8in  \setpage
%\nocon % omit table of contents
\datethis % print date on listing

\def\begitems{\medskip\bgroup\catcode`\*=13 \narrower\narrower}
\def\enditems{\par\egroup\medskip}
{\catcode`\*=13 \gdef*{\par\noindent\llap{$\bullet$\ }\ignorespaces}}



\N{1}{1}PROGRAM VLNA.
Program �te vstupn� textov� soubor a nahrazuje za specifikovan�mi
jednop�smenn�mi slovy (nap�.~v, k, u) mezery symbolem \uv{\.{\char126}}. To
zabr�n� p�i n�sledn�m zpracov�n� \TeX{}em zlomit ��dek na nevhodn�ch
m�stech, kter� jsou v rozporu s typografickou normou.

Program sest�v� z t�chto hlavn�ch celk�:
\Y\B\X3:Hlavi�kov� soubory k na�ten�\X\6
\X4:Glob�ln� deklarace\X\6
\X6:Pomocn� funkce\X\6
\X26:Vlnkovac� funkce \PB{\\{tie}}\X\6
\X5:Hlavn� program\X\par
\fi

\M{2}Definujeme \PB{\.{BANNER}}, co� je text, kter� se objevi p�i startu
programu a obsahuje ��slo verze programu.
Zde je n�zorn� vid�t, �e m�ch�n� dvou jazyk� se nevyhneme. P�i tisku
text� na termin�l nesm�me p�edpokl�dat, �e tam budou �esk� fonty.
V~t�to dokumentaci se setk�me se t�emi jazyky: angli�tinou (v�t�inou
v~k�du programu, cestinou v~/* koment���ch */ a �e�tinou jinde.
Tu cestinu si vynutil fakt, �e DOS-ovsk� varianta \.{tangle} a
\.{weave} se nesn�� s~akcentovan�mi p�smeny v~/* koment���ch */.
A~nyn� u� sl�ben� (v�cejazy�n�) \PB{\.{BANNER}}.
\Y\B\4\D$\.{BANNER}$ \5
\.{"This\ is\ program\ vln}\)\.{a,\ version\ 1.2,\ (c)\ }\)\.{1995,\ 2002\ Petr%
\ Olsa}\)\.{k\\n"}\par
\fi

\M{3}V programu jsou pou�ity knihovn� funkce, jej�ch� prototypy jsou
definov�ny ve t�ech standardn�ch hlavi�kov�ch souborech.
\Y\B\4\X3:Hlavi�kov� soubory k na�ten�\X${}\E{}$\6
\8\#\&{include} \.{<stdio.h>}\6
\8\#\&{include} \.{<string.h>}\6
\8\#\&{include} \.{<stdlib.h>}\par
\U1.\fi

\M{4}Definujeme konstanty pro n�vratov� k�d. \PB{\.{OK}} pro �sp�n� b�h,
\PB{\.{WARNING}} p�i v�skytu aspo� jedn� varovn� zpr�vy, \PB{\.{IO\_ERR}} pro
chybu
v~p��stupu ke vtupn�m nebo v�stupn�m soubor�m, \PB{\.{BAD\_OPTIONS}} pro
syntaktickou chybu na p��kazov� ��dce a \PB{\.{BAD\_PROGRAM}} pro p��pad
hav�rie programu. Ta by nem�la nikdy nastat. Prom�nn� \PB{\\{status}} bude
obsahovat n�vratov� k�d a prom�nn� \PB{\\{prog\_name}} bude ukazovat na text
nult�ho parametru p��kazov� ��dky.
\Y\B\4\D$\.{OK}$ \5
\T{0}\par
\B\4\D$\.{WARNING}$ \5
\T{1}\par
\B\4\D$\.{IO\_ERR}$ \5
\T{2}\par
\B\4\D$\.{BAD\_OPTIONS}$ \5
\T{3}\par
\B\4\D$\.{BAD\_PROGRAM}$ \5
\T{4}\par
\Y\B\4\X4:Glob�ln� deklarace\X${}\E{}$\6
\&{char} ${}{*}\\{prog\_name};{}$\6
\&{int} \\{status};\par
\As7, 15, 16, 20, 25\ETs33.
\U1.\fi

\M{5}Z�kladn� rozvr�en� funkce \PB{\\{main}}.
\Y\B\4\X5:Hlavn� program\X${}\E{}$\6
\&{int} \\{main}(\&{int} \\{argc}${},\39{}$\&{char} ${}{*}{*}\\{argv}){}$\1\1\2%
\2\6
${}\{{}$\1\6
\X9:Lok�ln� prom�nn� funkce \PB{\\{main}}\X;\6
${}\\{prog\_name}\K\\{argv}[\T{0}];{}$\6
${}\\{status}\K\.{OK};{}$\6
\X8:Na�ten� parametr� p��kazov�ho ��dku\X;\6
\&{if} ${}(\R\\{silent}){}$\1\5
${}\\{fprintf}(\\{stderr},\39\.{BANNER});{}$\2\6
\X21:Inicializace datov�ch struktur\X;\6
\X11:Zpracov�n� soubor�\X;\6
\&{return} \\{status};\6
\4${}\}{}$\2\par
\U1.\fi

\N{1}{6}Parametry p��kazov�ho ��dku.
Program �te z~p��kazov�ho ��dku postupn� (nepovinn�) parametry,
kter� za��naj� znakem \uv{\.{-}}. Pak n�sleduj� jm�na vstupn�ch a v�stupn�ch
soubor�.
\begitems
* \.{-f} \dots\ program pracuje jako filtr (viz sekce \PB{$\X11:Zpracov�n�
soubor�\X$}). Nen�-li tento parametr pou�it, program pracuje v tzv.
standardn�m re�imu, kdy jednotliv� soubory jsou vstupn� i v�stupn�.
* \.{-s} \dots\ program nevyp��e \PB{\.{BANNER}}, ani sumarizaci, ani varov�n�,
p�i nich� nen� program p�ed�asn� ukon�en.  V�echny tyto v�pisy
sm��uj� do \PB{\\{stderr}}, tak�e pokud program pracuje v re�imu \uv{filtr},
nen� nutn� tento parametr pou��t.
* \.{-r} \dots\ program ma�e pracovn� soubor (soubory), kter� vytv���
ve standardn�m re�imu (tj. nen� pou�it \.{-f}). V re�imu filter nem�
tento parametr vliv.
* \.{-v} \dots\ parametr definuje skupinu p�smen, kter� budou
interpretov�ny jako neslabi�n� p�edlo�ky.
Nap�. \.{-v KkSsVvZzOoUuAI}. Pokud nen� parametr uveden, je pou�ita
skupina uveden� v tomto p��klad�.
* \.{-m} \dots\ program neprov�d� kontrolu math/text m�d�, tj. vlnkuje i
uvnit� matematick�ho m�du \TeX{}u. (Implicite tam nevlnkuje).
* \.{-n} \dots\ prorgram neprov�d� kontrolu verbatim m�du, tj. vlnkuje i
uvnit� verbatim m�du definovan�m b�n�mi prost�ed�mi. Imlicite ve
verbatim prost�ed� nevlnkuje.
* \.{-l} \dots\ La\TeX{} re�im. P�i kontrole text-math-verbatim m�d� jsou
br�ny v �vahu dal�� sekvence, obvykl� v La\TeX{}ov�ch dokumentech.
* \.{-w} \dots\ WEB re�im. Ohrani�en� verbatim m�du je dopln�no znaky
pou��van�mi v dokumentech WEB (nap�. tento dokument). D�sledek: program
vlnkuje dokumenta�n� ��st ka�d� sekce, ale nikoli k�d.
\enditems

Definujeme funkci \PB{\\{printusage}}, kter� tiskne (p�i chyb�) stru�n� p�ehled
mo�n�ch parametr�. Nepoda�ilo se mi zjistit, jak se ve WEBu nap��e
kulturn� dlouh� string obsahuj�c� \.{\char92n} s form�tovac�mi
po�adavky. Byl jsem nucen to takto nehezky zapsat.
\Y\B\4\X6:Pomocn� funkce\X${}\E{}$\6
\&{static} \&{void} \\{printusage}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
${}\\{fprintf}(\\{stderr},\39\.{"usage:\ vlna\ [opt]\ [}\)\.{filenames]\\n"}%
\.{"\ \ opt\ -f\ :\ \ filter\ }\)\.{mode:\ file1\ file2\ ..}\)\.{.\
file1->file2\\n"}\.{"\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\)\.{\ \ \ \ \ \
file1\ \ \ \ \ \ \ ..}\)\.{.\ file1->stdout\\n"}\.{"\ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ }\)\.{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ..}\)\.{.\ stdin->stdout%
\\n"}\.{"\ \ \ \ \ \ \ \ \ \ \ \ nofilte}\)\.{r:\ file1\ [file2\ file}\)\.{3\
...]\ all\ are\ in/ou}\)\.{t\\n"}\.{"\ \ \ \ \ \ -s\ :\ \ silent:}\)\.{\ no\
messages\ to\ stde}\)\.{rr\\n"}\.{"\ \ \ \ \ \ -r\ :\ \ rmbacku}\)\.{p:\ if\
nofilter,\ remo}\)\.{ves\ temporary\ files\\}\)\.{n"}\.{"\ \ \ \ \ \ -v\
charset\ :\ }\)\.{\ set\ of\ lettres\ to\ a}\)\.{dd\ tie,\ default:\ KkS}\)%
\.{sVvZzOoUuAI\\n"}\.{"\ \ \ \ \ \ -m\ :\ \ nomath:}\)\.{\ ignores\ math\ modes%
\\}\)\.{n"}\.{"\ \ \ \ \ \ -n\ :\ \ noverb:}\)\.{\ ignores\ verbatim\ mo}\)%
\.{des\\n"}\.{"\ \ \ \ \ \ -l\ :\ \ LaTeX\ m}\)\.{ode\\n"}\.{"\ \ \ \ \ \ -w\ :%
\ \ web\ mod}\)\.{e\\n"});{}$\6
\4${}\}{}$\2\par
\As10, 17, 18, 19, 22, 23, 36, 38, 41, 42, 44, 45, 47, 49, 51, 52, 53\ETs55.
\U1.\fi

\M{7}Prom�nn� \PB{\\{isfilter}}, \PB{\\{silent}}, \PB{\\{rmbackup}}, \PB{%
\\{nomath}}, \PB{\\{noverb}},
\PB{\\{latex}}, resp. \PB{\\{web}} ��kaj�, �e je nastaven parametr \.{-f}, %
\.{-s},
\.{-r}, \.{-m}, \.{-n}, \.{-l}, resp. \.{-w}.  Prom�nn� \PB{\\{charset}}
ukazuje bu� na implicitn� skupinu znak� \PB{\\{charsetdefault}}, nebo (p�i
pou�it� parametru \.{-v}) na text uveden� v p��kazov�m ��dku.
\Y\B\4\X4:Glob�ln� deklarace\X${}\mathrel+\E{}$\6
\&{int} \\{isfilter}${}\K\T{0},{}$ \\{silent}${}\K\T{0},{}$ \\{rmbackup}${}\K%
\T{0},{}$ \\{nomath}${}\K\T{0},{}$ \\{noverb}${}\K\T{0},{}$ \\{web}${}\K%
\T{0},{}$ \\{latex}${}\K\T{0};{}$\6
\&{char} \\{charsetdefault}[\,]${}\K\.{"KkSsVvZzOoUuAI"};{}$\6
\&{char} ${}{*}\\{charset}\K\\{charsetdefault}{}$;\par
\fi

\M{8}\B\X8:Na�ten� parametr� p��kazov�ho ��dku\X${}\E{}$\6
\&{while} ${}(\\{argc}>\T{1}\W\\{argv}[\T{1}][\T{0}]\E\.{'-'}){}$\5
${}\{{}$\1\6
\&{if} ${}(\\{argv}[\T{1}][\T{2}]\I\T{0}){}$\1\5
${}\\{printusage}(\,),\39\\{exit}(\.{BAD\_OPTIONS});{}$\2\6
\&{switch} (\\{argv}[\T{1}][\T{1}])\5
${}\{{}$\1\6
\4\&{case} \.{'f'}:\5
${}\\{isfilter}\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'s'}:\5
${}\\{silent}\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'r'}:\5
${}\\{rmbackup}\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'v'}:\6
\&{if} ${}(\\{argc}<\T{2}){}$\1\5
${}\\{printusage}(\,),\39\\{exit}(\.{BAD\_OPTIONS});{}$\2\6
${}\\{argv}\PP;{}$\6
${}\\{argc}\MM;{}$\6
${}\\{charset}\K\\{argv}[\T{1}];{}$\6
\&{break};\6
\4\&{case} \.{'m'}:\5
${}\\{nomath}\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'n'}:\5
${}\\{noverb}\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'l'}:\5
${}\\{latex}\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'w'}:\5
${}\\{web}\K\T{1};{}$\6
\&{break};\6
\4\&{default}:\5
${}\\{printusage}(\,),\39\\{exit}(\.{BAD\_OPTIONS}){}$;\C{ nezn\'am\'y parametr
}\6
\4${}\}{}$\2\6
${}\\{argc}\MM;{}$\6
${}\\{argv}\PP;{}$\6
\4${}\}{}$\2\par
\U5.\fi

\N{1}{9}Zpracov�n� soubor�.  Parametr \PB{\.{MAXLEN}} definuje maxim�ln� mo�nou
d�lku jm�na souboru, kter� vytvo��me jako p�echodn�, nebo z�lohov�.
D�le deklarujeme prom�nn� typu \uv{stream}.
\Y\B\4\D$\.{MAXLEN}$ \5
\T{120}\par
\Y\B\4\X9:Lok�ln� prom�nn� funkce \PB{\\{main}}\X${}\E{}$\6
\&{FILE} ${}{*}\\{infile},{}$ ${}{*}\\{outfile};{}$\6
\&{char} \\{backup}[\.{MAXLEN}];\6
\&{int} \|j;\par
\U5.\fi

\M{10}Definujeme funkci pro v�pis chybov�ho hl�en� p�i ne�sp�n�m otev�en�
souboru.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{ioerr}(\&{char} ${}{*}\|f){}$\1\1\2\2\6
${}\{{}$\1\6
${}\\{fprintf}(\\{stderr},\39\.{"\%s:\ cannot\ open\ fil}\)\.{e\ \%s\\n"},\39%
\\{prog\_name},\39\|f);{}$\6
\4${}\}{}$\2\par
\fi

\M{11}Zp�sob zpracov�n� soubor� rozli��me podle re�imu dan�m p�ep�na�em \.{-f}.
\Y\B\4\X11:Zpracov�n� soubor�\X${}\E{}$\6
\&{if} (\\{isfilter}) \X12:Zpracov�n� v re�imu filter\X\6
\&{else} \X13:Zpracov�n� v�ech soubor� p��kazov� ��dky\X\par
\Q6.
\U5.\fi

\M{12}V re�imu \PB{$\\{isfilter}\E\T{1}$} je dal�� zpracov�n� z�visl� na po�tu
soubor� v
p��kazov� ��dce:
\begitems
* nula soubor� -- vstup je \PB{\\{stdin}} a v�stup je \PB{\\{stdout}},
* jeden soubor -- je vstupn�, v�stup je \PB{\\{stdout}},
* dva soubory -- prvn� je vstupn�, druh� v�stupn�,
* v�ce soubor� -- program skon�� s chybou.
\enditems
\Y\B\4\X12:Zpracov�n� v re�imu filter\X${}\E{}$\6
${}\{{}$\1\6
\&{if} ${}(\\{argc}>\T{3}){}$\1\5
${}\\{printusage}(\,),\39\\{exit}(\.{BAD\_OPTIONS});{}$\2\6
${}\\{infile}\K\\{stdin};{}$\6
${}\\{outfile}\K\\{stdout};{}$\6
\&{if} ${}(\\{argc}\G\T{2}){}$\1\5
${}\\{infile}\K\\{fopen}(\\{argv}[\T{1}],\39\.{"r"});{}$\2\6
\&{if} ${}(\\{infile}\E\NULL){}$\1\5
${}\\{ioerr}(\\{argv}[\T{1}]),\39\\{exit}(\.{IO\_ERR});{}$\2\6
\&{if} ${}(\\{argc}\E\T{3}){}$\1\5
${}\\{outfile}\K\\{fopen}(\\{argv}[\T{2}],\39\.{"w"});{}$\2\6
\&{if} ${}(\\{outfile}\E\NULL){}$\1\5
${}\\{ioerr}(\\{argv}[\T{2}]),\39\\{exit}(\.{IO\_ERR});{}$\2\6
\&{if} ${}(\\{argc}\G\T{2}){}$\1\5
${}\\{filename}\K\\{argv}[\T{1}];{}$\2\6
\&{else}\1\5
${}\\{filename}\K\NULL;{}$\2\6
${}\\{tie}(\\{infile},\39\\{outfile});{}$\6
\&{if} ${}(\\{outfile}\I\\{stdout}){}$\1\5
\\{fclose}(\\{outfile});\2\6
\&{if} ${}(\\{infile}\I\\{stdin}){}$\1\5
\\{fclose}(\\{infile});\2\6
\4${}\}{}$\2\par
\U11.\fi

\M{13}V~re�imu \PB{$\\{isfilter}\E\T{0}$} jsou jednotliv� soubory v~p��kazov�m
��dku
interpretov�ny jako vstupn� i v�stupn�. V�ce soubor� v~p��kazov�m ��dku m�
stejn� efekt, jako opakovan� vol�n� programu na jednotliv� soubory.
V~\UNIX/u lze tedy nap�. napsat \.{\jobname\ *.tex} a program dopln� vlnky do
v�ech soubor� s~p��ponou~\.{tex}. Toto neplat� v~DOSu, proto�e interpretace
masky je v~\UNIX/u starost� shellu a nikoli programu samotn�ho. N� program
masku nebude interpretovat. Je-li v~tomto re�imu nulov� po�et soubor�,
program se ukon�� s~chybou.
\Y\B\4\X13:Zpracov�n� v�ech soubor� p��kazov� ��dky\X${}\E{}$\6
${}\{{}$\1\6
\&{if} ${}(\\{argc}\E\T{1}){}$\1\5
${}\\{printusage}(\,),\39\\{exit}(\.{BAD\_OPTIONS});{}$\2\6
\&{while} ${}(\\{argc}>\T{1}){}$\5
${}\{{}$\1\6
${}\\{argc}\MM;{}$\6
${}\\{argv}\PP;{}$\6
\X14:P�ejmenuj vstup \PB{\\{argv}[\T{0}]} na \PB{\\{backup}} a otev�i jej jako %
\PB{\\{infile}}\X;\6
\&{if} ${}(\\{infile}\E\NULL){}$\5
${}\{{}$\1\6
\\{ioerr}(\\{argv}[\T{0}]);\6
\&{continue};\6
\4${}\}{}$\2\6
${}\\{outfile}\K\\{fopen}(\\{argv}[\T{0}],\39\.{"w"});{}$\6
\&{if} ${}(\\{outfile}\E\NULL){}$\5
${}\{{}$\1\6
\\{ioerr}(\\{argv}[\T{0}]);\6
${}\\{rename}(\\{backup},\39\\{argv}[\T{0}]);{}$\6
${}\\{status}\K\.{WARNING};{}$\6
\&{continue};\6
\4${}\}{}$\2\6
${}\\{filename}\K\\{argv}[\T{0}];{}$\6
${}\\{tie}(\\{infile},\39\\{outfile});{}$\6
${}\\{fclose}(\\{outfile}),\39\\{fclose}(\\{infile});{}$\6
\&{if} (\\{rmbackup})\1\5
\\{remove}(\\{backup});\2\6
\4${}\}{}$\2\6
\4${}\}{}$\2\par
\U11.\fi

\M{14}P�i \PB{$\\{isfilter}\E\T{0}$} program p�ejmenuje ka�d�  zpracov�van�
soubor tak, �e
zm�n� posledn� p�smeno n�zvu souboru na znak \.{\char126}. Tento
p�ejmenovan� soubor bude otev�en jako vstupn� a v�stupem bude p�vodn�
soubor. Vstupn� soubor p�i \PB{$\\{rmbackup}\E\T{0}$} z�stane zachov�n jako
z�loha.

Pro� vlnku nep�id�v�me na konec n�zvu souboru, ale m�n�me ji za posledn�
znak souboru? Proto�e chceme, aby program fungoval i v tak nemo�n�ch
syst�mech, jako je DOS.
\Y\B\4\X14:P�ejmenuj vstup \PB{\\{argv}[\T{0}]} na \PB{\\{backup}} a otev�i jej
jako \PB{\\{infile}}\X${}\E{}$\6
$\\{infile}\K\NULL;{}$\6
${}\|j\K\\{strlen}(\\{argv}[\T{0}])-\T{1};{}$\6
\&{if} ${}(\|j\G\.{MAXLEN}\V\\{argv}[\T{0}][\|j]\E\.{'\~'}){}$\5
${}\{{}$\1\6
\&{if} ${}(\R\\{silent}){}$\1\5
${}\\{fprintf}(\\{stderr},\39\.{"\%s:\ the\ conflict\ of}\)\.{\ file\ name\ \%s%
\\n"},\39\\{prog\_name},\39\\{argv}[\T{0}]);{}$\2\6
\4${}\}{}$\2\6
\&{else}\5
${}\{{}$\1\6
${}\\{strcpy}(\\{backup},\39\\{argv}[\T{0}]);{}$\6
${}\\{backup}[\|j]\K\.{'\~'};{}$\6
\\{remove}(\\{backup});\6
${}\|j\K\\{rename}(\\{argv}[\T{0}],\39\\{backup});{}$\6
\&{if} ${}(\|j\E\T{0}){}$\1\5
${}\\{infile}\K\\{fopen}(\\{backup},\39\.{"r"});{}$\2\6
\4${}\}{}$\2\par
\U13.\fi

\N{1}{15}Patterny.  Abychom mohli ��eln� definovat chov�n� programu
v~r�zn�ch situac�ch, zavedeme datovou strukturu \PB{\.{PATTERN}}. Zhruba
�e�eno, budeme sledovat vstup znak po znaku a pokud bude ��st vstupu
souhlasit s~definovan�m patternem, provedeme n�mi po�adovanou
akci. Nap��klad nej�ast�j�� aktivitu, p�id�n� vlnky uvnit� ��dku,
spust�me v~okam�iku, kdy vstupn� text odpov�d� patternu \uv{\.{\ (v\
p}}, kde \uv{\.{\ }} znamen� jedna nebo v�ce mezer a tabel�tor�,
\uv{\.{(}} je nula nebo v�ce otev�rac�ch z�vorek v�eho druhu,
\uv{\.{v}} znamen� jedno p�smeno z~mno�iny p�edlo�ek (viz \PB{\\{charset}}) a
\uv{\.{p}} zde znamen� libovoln� p�smeno. P��klad zde nen� zcela p�esn�.
P�esn� jsou v�echny patterny pro n� program definov�ny v~z�v�re�n�ch
sekc�ch tohoto pov�d�n�.

Pattern bude znamenat kone�nou sekvenci tzv. pozic patternu (\PB{\.{PATITEM}}).
Cykly uvnit� pozic pro jednoduchost nep�ipust�me. Ka�d� pozice obsahuje
�et�zec znak�, uva�ovan� pro danou pozici (v~p��kladu pozice~\uv{\.{\ }} by
obsahovala mezeru a tabel�tor, zat�mco pozice \.{v} odpov�d� \PB{\\{charset}}).
Ka�d� pozice m� sv�j p�ep�na� (\PB{\\{flag}}), kter� obsahuje informaci o~tom,
zda shodu testovan�ho znaku s~n�kter�m prvkem v~mno�in� znak�
budeme pova�ovat za �sp�ch �i ne�sp�ch a zda pozice se ve zkouman�m
�et�zci m��e vyskytovat pr�v� jednou nebo opakovan�. Jako druh� p��pad
sta�� implementovat \uv{nula nebo v�ce} proto�e \uv{jedna nebo v�ce} lze
popsat pomoc� dvou pozic, prvn� \uv{pr�v� jednou} a n�sleduj�c� \uv{nula
nebo v�ce}. Jednotliv� pozice jsou z�et�zeny ukazatelem \PB{\\{next}}, posledn�
pozice m� \PB{$\\{next}\E\NULL$}. Stejn� tak jednotliv� patterny budeme
sestavovat do seznam� a budou rovn� z�et�zeny ukazatelem \PB{\\{next}}.

Pattern krom� �et�zu pozic obsahuje ukazatel na funkci (proceduru) \PB{%
\\{proc}},
kter� se m� vykonat v~p��pad�, �e testovan� �et�zec vyhovuje patternu.

\Y\B\4\D$\.{ONE}$ \5
\T{1}\C{ flag: prave jeden vyskyt }\par
\B\4\D$\.{ANY}$ \5
\T{2}\C{ flag: nula nebo vice }\par
\B\4\D$\.{ONE\_NOT}$ \5
${-}{}$\T{1}\C{ flag: prave jednou, znak nesmi byt v mnozine }\par
\B\4\D$\.{ANY\_NOT}$ \5
${-}{}$\T{2}\C{ flag: nula nebo vice, znak nesmi byt v mnozine }\par
\Y\B\4\X4:Glob�ln� deklarace\X${}\mathrel+\E{}$\6
\&{typedef} \&{struct} \&{PATITEM} ${}\{{}$\C{ jedna pozice patternu }\1\6
\&{char} ${}{*}\\{str}{}$;\C{ seznam znaku na teto pozici }\6
\&{int} \\{flag};\C{ vyznam seznamu znaku }\6
\&{struct} \&{PATITEM} ${}{*}\\{next}{}$;\C{ nasledujici pozice patternu }\2\6
${}\}{}$ \&{PATITEM};\6
\&{typedef} \&{struct} \&{PATTERN} ${}\{{}$\C{ jeden pattern }\1\6
\&{PATITEM} ${}{*}\\{patt}{}$;\C{ ukazatel na prvni pozici }\7
${}\&{void}({*}\\{proc})(\&{void}){}$;\C{ procedura spustena pri souhlasu
patternu }\7
\&{struct} \&{PATTERN} ${}{*}\\{next}{}$;\C{ nasledujici v seznamu vsech
patternu }\2\6
${}\}{}$ \&{PATTERN};\par
\fi

\M{16}Deklarujeme n�kter� glob�ln� prom�nn� pro pr�ci s~patterny. \PB{\\{lapi}}
je pole
obsahuj�c� ukazatele na aktu�ln� pozice v~otev�en�ch patternech. ��k�me,
�e \uv{pattern je otev�en}, pokud zkouman� �et�zec s~n�m {\it za��n�\/}
souhlasit. Pattern se uzav�e, pokud nastane jedna ze dvou mo�nost�:
zkouman� �et�zec s~m�m souhlas� a� do konce (v~takov�m p��pad� se provede
procedura \PB{\\{proc}}), nebo p�i vy�et�ov�n� dal��ch znak� ze zkouman�ho
�et�zce p�estane �et�zec s~patternem souhlasit.

V~dan� chv�li m��e b�t pattern otev�en n�kolikr�t. Nap�. pattern \.{abac}
je p�i stringu \.{aba} p�i v�skytu druh�ho \.{a} otev�en podruh�. Proto
pole obsahuje ukazatele na pr�v� aktu�ln� pozici patternu a nikoli na
pattern jako takov�.

V~poli \PB{\\{lapi}} budou na po��tku sam� \PB{$\NULL$} (to se p�i p�ekladu
inicializuje
samo) a p�emaz�n� ukazatele na pozici konstantou \PB{$\NULL$} budeme pova�ovat
za zav�en� patternu. Vedle pole \PB{\\{lapi}} soum�rn� udr�ujeme pole \PB{%
\\{lapt}},
do n�ho� budeme ukl�dat ukazatele na odpov�daj�c� otev�en� pattern. Tuto
informaci pou�ijeme v~p��pad�, �e pot�ebujeme nap�, zn�t \PB{\\{proc}}
patternu.

\PB{\\{listpatt}} bude ukazovat na za��tek aktu�ln�ho seznamu pattern�. Seznamy
budeme m�t dva. Jeden se pou�ije, nach�z�me-li se mimo koment�� a druh�
v~p��pad�, �e se nach�z�me v~prostoru \TeX{}ovsk�ho koment��e (tj. za
procentem). Starty t�chto seznam� pattern� jsou \PB{\\{normallist}} a
\PB{\\{commentlist}} a aktivn� \PB{\\{listpatt}} m� v�dy jednu z~t�chto dvou
hodnot.

Prom�nn� \PB{\\{lastpt}} a \PB{\\{lastpi}} pou�ijeme pro budov�n� �et�zov�
struktury
pattern�.

Prom�nn� \PB{\|c} obsahuje pr�v� testovan� znak ze vstupu (kter� se rovn�
p�ep��e do bufferu \PB{\\{buff}}). Z~bufferu ob�as ukl�d�me data do v�stupn�ho
proudu. D�l�me to ale v�dy jen v~okam�iku, kdy nen� otev�en ��dn�
pattern. Tehdy toti� \uv{nehroz�} situace, �e by n�jak� procedura vyvolan�
souhlasem patternu po�adovala v~tomto bufferu n�jak� zm�ny se zp�tnou
platnost�. O~vypr�zdn�n� bufferu se za�neme zaj�mat a� v~okam�iku, kdy je
zapln�n aspo� na hodnotu \PB{\.{BUFI}}, abychom proceduru p�episu bufferu do
v�stupn�ho proudu neaktivovali zbyte�n� �asto.
\Y\B\4\D$\.{MAXPATT}$ \5
\T{200}\C{ maximalni pocet patternu }\par
\B\4\D$\.{MAXBUFF}$ \5
\T{500}\C{ velikost bufferu pro operace }\par
\B\4\D$\.{BUFI}$ \5
\T{300}\C{ velikost stredniho zaplneni }\par
\Y\B\4\X4:Glob�ln� deklarace\X${}\mathrel+\E{}$\6
\&{PATITEM} ${}{*}\\{lapi}[\.{MAXPATT}]{}$;\C{ pole ukazatelu na aktualni
pozice }\6
\&{PATTERN} ${}{*}\\{lapt}[\.{MAXPATT}]{}$;\C{ pole odpovidajicich ukazatelu na
patterny }\6
\&{PATTERN} ${}{*}\\{listpatt},{}$ ${}{*}\\{normallist},{}$ ${}{*}%
\\{commentlist},{}$ ${}{*}\\{pt},{}$ ${}{*}\\{lastpt}\K\NULL;{}$\6
\&{PATITEM} ${}{*}\\{lastpi}\K\NULL;{}$\6
\&{char} \|c;\C{ zrovna nacetny znak }\6
\&{char} \\{buff}[\.{MAXBUFF}];\C{ prechodny buffer }\6
\&{int} \\{ind};\C{ aktualni pozice prechodneho bufferu }\par
\fi

\M{17}Nyn� definujeme pomocn� funkce \PB{\\{setpattern}}, \PB{\\{setpi}} a \PB{%
\\{normalpattern}}.
Tyto funkce alokuj� pam� pomoc� standardn� funkce \PB{\\{malloc}}. Abychom
mohli
ohl�dat p��padnou chybu p�i alokaci, budeme allokovat pam� zprost�edkovan�
pomoc� funkce \PB{\\{myalloc}}.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} ${}{*}{}$\\{myalloc}(\&{int} \\{size})\1\1\2\2\6
${}\{{}$\1\6
\&{void} ${}{*}\|p;{}$\7
${}\|p\K\\{malloc}(\\{size});{}$\6
\&{if} ${}(\|p\E\NULL){}$\5
${}\{{}$\1\6
${}\\{fprintf}(\\{stderr},\39\.{"\%s,\ no\ memory,\ mall}\)\.{oc\ failed\\n"},%
\39\\{prog\_name});{}$\6
\\{exit}(\.{BAD\_PROGRAM});\6
\4${}\}{}$\2\6
\&{return} \|p;\6
\4${}\}{}$\2\par
\fi

\M{18}Funkce \PB{\\{setpattern}} alokuje pam�ov� m�sto struktury \PB{%
\&{PATTERN}} a napoj�
ji pomoc� prom�nn� \PB{\\{lastpt}} na u� alokovan� �et�z pattern�.
Vr�t� ukazatel na nov� alokovan� m�sto. Jednotliv� pozice patternu se mus�
n�sledovn� alokovat pomoc� \PB{\\{setpi}}.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{PATTERN} ${}{*}\\{setpattern}(\&{void}({*}\\{proc})(\&{void})){}$%
\1\1\2\2\6
${}\{{}$\1\6
\&{PATTERN} ${}{*}\\{pp};{}$\7
${}\\{pp}\K\\{myalloc}(\&{sizeof}(\&{PATTERN}));{}$\6
${}\\{pp}\MG\\{proc}\K\\{proc};{}$\6
${}\\{pp}\MG\\{next}\K\NULL;{}$\6
${}\\{pp}\MG\\{patt}\K\NULL;{}$\6
\&{if} ${}(\\{lastpt}\I\NULL){}$\1\5
${}\\{lastpt}\MG\\{next}\K\\{pp};{}$\2\6
${}\\{lastpt}\K\\{pp};{}$\6
${}\\{lastpi}\K\NULL;{}$\6
\&{return} \\{pp};\6
\4${}\}{}$\2\par
\fi

\M{19}Funkce \PB{\\{setpi}} alokuje pam�ov� m�sto pro jednu pozici patternu.
Provede
z�et�zen� tak, aby prvn� pozice �et�zu pozic byla zaznamen�na v polo�ce
\PB{\\{patt}} ve struktu�e \PB{\&{PATTERN}} a dal�� byly prov�z�ny polo�kou %
\PB{\\{next}} ve
struktu�e \PB{\&{PATITEM}}. Posledn� pozice m� \PB{$\\{next}\E\NULL$}.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{setpi}(\&{char} ${}{*}\\{str},\39{}$\&{int} \\{flag})\1%
\1\2\2\6
${}\{{}$\1\6
\&{PATITEM} ${}{*}\|p;{}$\7
${}\|p\K\\{myalloc}(\&{sizeof}(\&{PATITEM}));{}$\6
${}\|p\MG\\{str}\K\\{str};{}$\6
${}\|p\MG\\{flag}\K\\{flag};{}$\6
${}\|p\MG\\{next}\K\NULL;{}$\6
\&{if} ${}(\\{lastpi}\E\NULL){}$\1\5
${}\\{lastpt}\MG\\{patt}\K\|p;{}$\2\6
\&{else}\1\5
${}\\{lastpi}\MG\\{next}\K\|p;{}$\2\6
${}\\{lastpi}\K\|p;{}$\6
\4${}\}{}$\2\par
\fi

\M{20}P�ipravme si p�du pro funkci \PB{\\{normalpattern}}. Tato funkce alokuje
strukturu pro jeden pattern v�etn� pozic patternu na z�klad� vstupn�ho
stringu. Ka�d� pozice patternu obsahuje v~mno�in� znak� jedin� znak a m�
\PB{$\\{flag}\K\.{ONE}$}. Znaky ve vstupn�m stringu odpov�daj� po �ad�
jednotliv�m
pozic�m. Vytvo�� se vlastn� jak�si absolutn� pattern, tj. testovan� �et�zec
se mus� p�esn� shodovat s~uveden�m stringem. V�jimku tvo�� znak \PB{\.{"."}},
kter� se interpretuje jako nula nebo v�ce mezer. Chceme-li te�ku
vnutit do patternu, nap��eme dv� te�ky za sebou.

Nejd��ve deklarujeme pole v�ech mo�n�ch jednop�smenn�ch string�.
\Y\B\4\X4:Glob�ln� deklarace\X${}\mathrel+\E{}$\6
\&{char} \\{strings}[\T{512}];\6
\&{int} \|i;\par
\fi

\M{21}Inicializujeme toto pole (znak, nula, znak, nula, atd...).
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\E{}$\6
\&{for} ${}(\|i\K\T{0};{}$ ${}\|i<\T{256};{}$ ${}\|i\PP){}$\5
${}\{{}$\1\6
${}\\{strings}[\T{2}*\|i]\K{}$(\&{char}) \|i;\6
${}\\{strings}[\T{2}*\|i+\T{1}]\K\T{0};{}$\6
\4${}\}{}$\2\par
\As34, 37, 39, 40, 43, 46, 48, 50\ETs54.
\U5.\fi

\M{22}Definujme funkci \PB{\\{normalpattern}}.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{PATTERN} ${}{*}\\{normalpattern}(\&{void}({*}\\{proc})(\&{void}),%
\39{}$\&{const} \&{char} ${}{*}\\{str}){}$\1\1\2\2\6
${}\{{}$\1\6
\&{PATTERN} ${}{*}\\{pp};{}$\6
\&{int} \|j${}\K\T{0};{}$\7
${}\\{pp}\K\\{setpattern}(\\{proc});{}$\6
\&{while} (\\{str}[\|j])\5
${}\{{}$\1\6
\&{if} ${}(\\{str}[\|j]\E\.{'.'}){}$\5
${}\{{}$\1\6
${}\|j\PP;{}$\6
\&{if} ${}(\\{str}[\|j]\I\.{'.'}){}$\5
${}\{{}$\1\6
${}\\{setpi}(\\{blankscr},\39\.{ANY});{}$\6
\&{continue};\6
\4${}\}{}$\2\6
\4${}\}{}$\2\6
${}\\{setpi}({\AND}\\{strings}{}$[(\&{unsigned} \&{char}) \\{str}[\|j]${}*%
\T{2}],\39\.{ONE});{}$\6
${}\|j\PP;{}$\6
\4${}\}{}$\2\6
\&{return} \\{pp};\6
\4${}\}{}$\2\par
\fi

\M{23}Funkce \PB{\\{match}}. Definujeme funkci, kter� na z�klad� hodnoty znaku %
\PB{\|c}
(prom�nn� \PB{\|c} je definov�na jako glob�ln�), a pozice patternu \PB{\|p}
(parametr
funkce) vr�t� informaci o tom, zda znak souhlas� s patternem. Z�porn� ��sla
\PB{\.{FOUND}}, resp. \PB{\.{NOFOUND}} znamenaj�, �e je t�eba uzav��t pattern s
t�m, �e
vzor odpov�d�, resp. neodpov�d� patternu. Nez�porn� ��slo vr�t� v p��pad�,
�e zkouman� vstup st�le souhlas� s patternem, ale nen� je�t�
rozhodnuto. Velikost n�vratov� hodnoty v takov�m p��pad� ud�v�, o kolik
pozic je t�eba se posunout v patternu, abychom m�li ukazatel na pozici
patternu v souhlase s novou situac�, zp�sobenou znakem \PB{\|c}.

Pokud je \PB{\|c} v mno�in� znak� pro danou pozici \PB{$\|p\MG\\{str}$}, bude %
\PB{$\|m\E\T{1}$}, jinak
je \PB{$\|m\E{-}\T{1}$}. Pokud t�mto ��slem pron�sob�me hodnotu \PB{$\|p\MG%
\\{flag}$}, nemus�me
v�tven� podle \PB{$\|p\MG\\{flag}$} programovat dvakr�t. Hodnoty \PB{\\{flag}}
jsou toti�
symetrick� podle nuly, nap�. \PB{$\.{ANY}\E{-}\.{ANY\_NOT}$}.
\Y\B\4\D$\.{FOUND}$ \5
${-}{}$\T{1}\par
\B\4\D$\.{NOFOUND}$ \5
${-}{}$\T{2}\par
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{int} \\{match}(\&{PATITEM} ${}{*}\|p){}$\1\1\2\2\6
${}\{{}$\1\6
\&{int} \|m;\7
\&{if} ${}(\\{strchr}(\|p\MG\\{str},\39\|c)\I\NULL){}$\1\5
${}\|m\K\T{1}{}$;\C{ Znak nalezen }\2\6
\&{else}\1\5
${}\|m\K{-}\T{1}{}$;\C{ Znak nenalezen }\2\6
\&{switch} ${}(\|m*\|p\MG\\{flag}){}$\5
${}\{{}$\1\6
\4\&{case} \.{ANY}:\5
\&{return} \T{0};\C{ Souhas, neni nutny posun }\6
\4\&{case} \.{ONE}:\6
\&{if} ${}(\|p\MG\\{next}\E\NULL){}$\1\5
\&{return} \.{FOUND};\2\6
\&{return} \T{1};\C{ Souhas, nutny posun o 1 }\6
\4\&{case} \.{ONE\_NOT}:\5
\&{return} \.{NOFOUND};\C{ Nesouhlas }\6
\4\&{case} \.{ANY\_NOT}:\5
\X24:Vra� hodnotu podle n�sleduj�c� pozice patternu\X;\6
\4${}\}{}$\2\6
\&{return} \T{0};\C{ Tady bychom nikdy nemeli byt, return pro potlaceni
varovani }\6
\4${}\}{}$\2\par
\fi

\M{24}O kolik pozic je t�eba se posunout a s jak�m v�sledkem zjist�me
rekurzivn�m vol�n�m funkce \PB{\\{match}}.
\Y\B\4\X24:Vra� hodnotu podle n�sleduj�c� pozice patternu\X${}\E{}$\6
\&{switch} ${}(\|m\K\\{match}(\|p\MG\\{next})){}$\5
${}\{{}$\1\6
\4\&{case} \.{NOFOUND}:\5
\&{return} \.{NOFOUND};\6
\4\&{case} \.{FOUND}:\5
\&{return} \.{FOUND};\6
\4\&{default}:\5
\&{return} \T{1}${}+\|m;{}$\6
\4${}\}{}$\2\par
\U23.\fi

\N{1}{25}Vlnkovac� funkce.
Nejprve p�iprav�me glob�ln� deklarace pro \uv{vlnkovac�} funkci \PB{\\{tie}}.
Funkce \PB{\\{tie}} \uv{ovlnkuje} vstupn� soubor \PB{\\{infile}} a vytvo��
soubor
\PB{\\{outfile}}.  P�i \PB{$\\{silent}\K\T{0}$} tiskne z�v�re�nou zpr�vu o
zpracov�n�. V t�to
zpr�v� se objev� jm�no souboru, kter� se funkce \uv{dozv�} prost�ednictv�m
glob�ln� prom�nn� \PB{\\{filename}}. Prom�nn� \PB{\\{numline}} po��t� ��dky,
prom�nn�
\PB{\\{numchanges}} s��t� zm�ny, tj. po�et dopln�n�ch vlnek.
Prom�nn� \PB{\\{mode}} nab�v� n�kter� z hodnot \PB{\.{TEXTMODE}}, \PB{%
\.{MATHMODE}},
\PB{\.{DISPLAYMODE}} a \PB{\.{VERBMODE}} podle stavu ve �ten�m textu.
\Y\B\4\D$\.{TEXTMODE}$ \5
\T{0}\par
\B\4\D$\.{MATHMODE}$ \5
\T{1}\par
\B\4\D$\.{DISPLAYMODE}$ \5
\T{2}\par
\B\4\D$\.{VERBMODE}$ \5
\T{3}\par
\Y\B\4\X4:Glob�ln� deklarace\X${}\mathrel+\E{}$\6
\&{char} ${}{*}\\{filename}{}$;\C{ jmeno zpracovavaneho souboru }\6
\&{long} \&{int} \\{numline}${},{}$ \\{numchanges};\C{ pro zaverecnou
statistiku }\6
\&{int} \\{mode};\par
\fi

\M{26}Nyn� definujeme vlnkovac� funkci \PB{\\{tie}}. Ve�ker� �innost se op�r� o
strukturu pattern�. V�hodn� je (z d�vodu rychlosti) \uv{natvrdo} zde
implementovat jen p�ep�n�n� mezi stavem �ten� z oblasti koment��e
(\PB{$\\{listpatt}\E\\{commentlist}$}) a mimo koment�� (\PB{$\\{listpatt}\E%
\\{normallist}$});
\Y\B\4\X26:Vlnkovac� funkce \PB{\\{tie}}\X${}\E{}$\6
\&{static} \&{void} \\{tie}(\&{FILE} ${}{*}\\{input},\39{}$\&{FILE} ${}{*}%
\\{output}){}$\1\1\2\2\6
${}\{{}$\1\6
\&{int} \\{ap};\C{ ap je pocet otevrenych patternu }\6
\&{register} \&{int} \|k${},{}$ \|m${},{}$ \|n;\6
\&{int} \\{ic};\6
\&{PATTERN} ${}{*}\\{pp};{}$\6
\&{PATITEM} ${}{*}\\{pi};{}$\7
\X27:Inicializace prom�nn�ch p�i startu funkce \PB{\\{tie}}\X;\6
\&{while} ${}(\R\\{feof}(\\{input})){}$\5
${}\{{}$\1\6
\X30:Otev�i nov� patterny\X;\6
\&{if} ${}(\\{ap}\E\T{0}\W\\{ind}>\.{BUFI}\W\|c\I\.{'\\\\'}){}$\1\5
\X28:Vypr�zdni buffer\X;\2\6
\&{if} ${}(\\{ind}\G\.{MAXBUFF}){}$\5
${}\{{}$\1\6
${}\\{fprintf}(\\{stderr},\39\.{"Operating\ buffer\ ov}\)\.{erflow,\ is\
anything\ }\)\.{wrong?\\n"});{}$\6
\\{exit}(\.{BAD\_PROGRAM});\6
\4${}\}{}$\2\6
\&{if} ${}((\\{ic}\K\\{getc}(\\{input}))\E\.{EOF}{}$)\C{ opravil Cejka Rudolf }%
\1\6
\&{break};\2\6
${}\\{buff}[\\{ind}\PP]\K\|c\K\\{ic};{}$\6
\&{if} ${}(\|c\E\.{'\\n'}){}$\1\5
${}\\{numline}\PP,\39\\{listpatt}\K\\{normallist};{}$\2\6
\&{if} ${}(\|c\E\.{'\%'}\W\\{mode}\I\.{VERBMODE}\W\\{buff}[\\{ind}-\T{2}]\I\.{'%
\\\\'}){}$\1\5
${}\\{listpatt}\K\\{commentlist};{}$\2\6
\X29:Projdi otev�en� patterny\X;\6
\4${}\}{}$\2\6
\X28:Vypr�zdni buffer\X;\6
\&{if} ${}(\R\\{web}){}$\1\5
\\{checkmode}(\,);\C{ zaverecna kontrola modu }\2\6
\&{if} ${}(\R\\{silent}){}$\1\5
\X32:Tiskni z�v�re�nou zpr�vu\X;\2\6
\4${}\}{}$\2\par
\U1.\fi

\M{27}\B\X27:Inicializace prom�nn�ch p�i startu funkce \PB{\\{tie}}\X${}\E{}$\6
\&{for} ${}(\|k\K\T{0};{}$ ${}\|k<\.{MAXPATT};{}$ ${}\|k\PP){}$\1\5
${}\\{lapi}[\|k]\K\NULL;{}$\2\6
${}\|c\K\.{'\\n'};{}$\6
${}\\{buff}[\T{0}]\K\\{mode}\K\\{ap}\K\T{0};{}$\6
${}\\{ind}\K\T{1};{}$\6
${}\\{numline}\K\T{1};{}$\6
${}\\{numchanges}\K\T{0};{}$\6
${}\\{mode}\K\.{TEXTMODE}{}$;\par
\A35.
\U26.\fi

\M{28}P�i manipulaci s bufferem byl pou�it jeden trik. Ve�ker� na�ten� znaky
za��naj� a� od \PB{\\{buff}[\T{1}]}, zat�mco \PB{\\{buff}[\T{0}]} je rovno
nule. Je to proto, �e
n�kter� algoritmy se vrac� o jeden znak zp�t za sv�j pattern, aby zjistily,
zda tam nen� symbol \uv{\.{\char92}} (nap��klad na v�skyt sekvence
\.{\char92\char37} je t�eba reagovat jinak, ne� na v�skyt oby�ejn�ho
procenta). Kdybychm zaza�ali od \PB{\\{buff}[\T{0}]}, v n�kter�ch situac�ch
bychom se ptali, zda \PB{$\\{buff}[{-}\T{1}]\E\.{'\\\\'}$}, tj. sahali bychom
na neo�et�en�
m�sto v pam�ti.
\Y\B\4\X28:Vypr�zdni buffer\X${}\E{}$\6
${}\{{}$\1\6
${}\\{buff}[\\{ind}]\K\T{0};{}$\6
${}\\{fputs}({\AND}\\{buff}[\T{1}],\39\\{output});{}$\6
${}\\{ind}\K\T{1};{}$\6
\4${}\}{}$\2\par
\U26.\fi

\M{29}P�i proch�zen� otev�en�mi patterny posunujeme v poli \PB{\\{lapi}} pozice
jednotliv�ch pattern� podle pokyn� funkce \PB{\\{match}}, p��padn� pattern
zav�eme
a p��padn� vyvol�me proceduru patternu.

N�kter� patterny v poli \PB{\\{lapi}} u� mohou b�t zav�eny, tak�e je nutno s
t�mto
polem pracovat jako s jak�msi d�rav�m s�rem.
\Y\B\4\X29:Projdi otev�en� patterny\X${}\E{}$\6
$\|n\K\\{ap};{}$\6
${}\|k\K\T{0};{}$\6
\&{while} (\|n)\5
${}\{{}$\1\6
\&{while} ${}(\\{lapi}[\|k]\E\NULL){}$\1\5
${}\|k\PP{}$;\C{ zastav se na prvnim ukazateli na pattern }\2\6
\&{switch} ${}(\|m\K\\{match}(\\{lapi}[\|k])){}$\5
${}\{{}$\1\6
\4\&{case} \.{FOUND}:\5
${}({*}\\{lapt}[\|k]\MG\\{proc})(\,){}$;\C{ Pattern nalezen, spustit proceduru
}\6
\4\&{case} \.{NOFOUND}:\5
${}\\{lapi}[\|k]\K\NULL{}$;\C{ Deaktivace patternu }\6
${}\\{ap}\MM;{}$\6
\&{break};\6
\4\&{default}:\6
\&{while} ${}(\|m\MM){}$\1\5
${}\\{lapi}[\|k]\K\\{lapi}[\|k]\MG\\{next}{}$;\C{ dalsi pozice patternu }\2\6
\4${}\}{}$\2\6
${}\|k\PP;{}$\6
${}\|n\MM;{}$\6
\4${}\}{}$\2\par
\U26.\fi

\M{30}P�i otev�r�n� nov�ch pattern�, kter� nejsou v tuto chv�li zablokov�ny,
se hned vypo��d�me s takov�mi patterny, kter� n�m d�vaj� rovnou odpov��
typu \PB{\.{FOUND}} nebo \PB{\.{NOFOUND}}. V takov�ch p��padech ani nezan��me
ukazatel
na pozici do pole \PB{\\{lapi}}.
\Y\B\4\X30:Otev�i nov� patterny\X${}\E{}$\6
$\\{pp}\K\\{listpatt};{}$\6
\&{while} ${}(\\{pp}\I\NULL){}$\5
${}\{{}$\1\6
\&{switch} ${}(\|m\K\\{match}(\\{pp}\MG\\{patt})){}$\5
${}\{{}$\1\6
\4\&{case} \.{FOUND}:\5
${}({*}\\{pp}\MG\\{proc})(\,){}$;\C{ spustit proceduru }\6
\4\&{case} \.{NOFOUND}:\5
\&{break};\6
\4\&{default}:\5
\X31:Vytvo� ukazatel na nov� pattern a \PB{\&{break}}\X;\6
\4${}\}{}$\2\6
${}\\{pp}\K\\{pp}\MG\\{next};{}$\6
\4${}\}{}$\2\par
\U26.\fi

\M{31}Nen�-li hned zn�ma odpov��, zda pattern vyhovuje �i nikoli,
p�ekontrolujeme nejd��ve, zda u� nen� pattern ve stejn� pozici otev�en�.
Pak najdeme prvn� \uv{d�ru} v tabulce \PB{\\{lapi}} a tam uhn�zd�me nov�
ukazatel
na pozici v patternu.
\Y\B\4\X31:Vytvo� ukazatel na nov� pattern a \PB{\&{break}}\X${}\E{}$\6
$\\{pi}\K\\{pp}\MG\\{patt};{}$\6
\&{while} ${}(\|m\MM){}$\1\5
${}\\{pi}\K\\{pi}\MG\\{next};{}$\2\6
${}\|n\K\\{ap};{}$\6
${}\|k\K\T{0};{}$\6
\&{while} (\|n)\5
${}\{{}$\1\6
\&{if} ${}(\\{lapi}[\|k]\E\\{pi}){}$\1\5
\&{break};\2\6
\&{if} ${}(\\{lapi}[\|k\PP]\I\NULL){}$\1\5
${}\|n\MM;{}$\2\6
\4${}\}{}$\2\6
\&{if} ${}(\R\|n){}$\5
${}\{{}$\1\6
${}\|k\K\T{0};{}$\6
\&{while} ${}(\\{lapi}[\|k]\I\NULL){}$\1\5
${}\|k\PP;{}$\2\6
\&{if} ${}(\|k\G\.{MAXPATT}){}$\5
${}\{{}$\1\6
${}\\{fprintf}(\\{stderr},\39\.{"I\ cannot\ allocate\ p}\)\.{p,\ is\ anything\
wrong}\)\.{?\\n"});{}$\6
\\{exit}(\.{BAD\_PROGRAM});\6
\4${}\}{}$\2\6
${}\\{lapt}[\|k]\K\\{pp};{}$\6
${}\\{lapi}[\|k]\K\\{pi};{}$\6
${}\\{ap}\PP;{}$\6
\4${}\}{}$\2\par
\U30.\fi

\M{32}Posledn� v�c� ve funci \PB{\\{tie}} je tisk z�v�re�n� statistiky
zpracov�n�.
\Y\B\4\X32:Tiskni z�v�re�nou zpr�vu\X${}\E{}$\6
$\\{fprintf}(\\{stderr},\39\.{"\~\~\~\ file:\ \%s\\t\ \ lin}\)\.{es:\ \%ld,\
changes:\ \%l}\)\.{d\\n"},\39\\{filename},\39\\{numline},\39\\{numchanges}){}$;%
\par
\U26.\fi

\N{1}{33}Inicializace pattern�.
Po vytvo�en� p�edchoz�ho k�du op�raj�c�ho se o~patterny m�me nyn� v~ruce
pom�rn� siln� n�stroj na definov�n� r�zn�ch �innost� programu prost�m
vytvo�en�m patternu a p��slu�n� jeho procedury. Pokud budeme cht�t
v~budoucnu n�jak� rys programu p�idat, pravd�podobn� to bude snadn�.

Nejprve deklarujeme n�kter� �asto pou��van� skupiny znak� v~patternech.

\Y\B\4\X4:Glob�ln� deklarace\X${}\mathrel+\E{}$\6
\&{char} \\{tblanks}[\,]${}\K\.{"\ \~\\t"};{}$\6
\&{char} \\{blanks}[\,]${}\K\.{"\ \\t"};{}$\6
\&{char} \\{blankscr}[\,]${}\K\.{"\ \\t\\n"};{}$\6
\&{char} \\{tblankscr}[\,]${}\K\.{"\ \~\\t\\n"};{}$\6
\&{char} \\{nochar}[\,]${}\K\.{"\%\~\\n"};{}$\6
\&{char} \\{cr}[\,]${}\K\.{"\\n"};{}$\6
\&{char} \\{prefixes}[\,]${}\K\.{"[(\{\~"};{}$\6
\&{char} \\{dolar}[\,]${}\K\.{"\$"};{}$\6
\&{char} \\{backslash}[\,]${}\K\.{"\\\\"};{}$\6
\&{char} \\{openbrace}[\,]${}\K\.{"\{"};{}$\6
\&{char} \\{letters}[\,]${}\K\.{"abcdefghijklmnopqrs}\)\.{tuvwxyzABCDEFGHIJKLM}%
\)\.{NOPQRSTUVWXYZ"};{}$\6
\&{PATTERN} ${}{*}\\{vlnkalist},{}$ ${}{*}\\{mathlist},{}$ ${}{*}%
\\{parcheck},{}$ ${}{*}\\{verblist}{}$;\par
\fi

\M{34}Za�neme definic� nej�ast�ji pou��van�ho patternu na vlnkov�n� uvnit�
��dku. P�ipome�me, �e opakovan� vol�n� funkce \PB{\\{setpattern}} vytv���
intern�
seznam pattern�, p�i�em� o~jejich propojen� se nemus�me starat. Vyzvedneme
si z~n�vratov�ho k�du funkce pouze ukazatel na prvn� polo�ku seznamu
\PB{\\{normallist}}. Stejn� tak opakovan� vol�n� funkce \PB{\\{setpi}} vytv���
seznam
pozic pro naposledy deklarovan� pattern.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
$\\{vlnkalist}\K\\{setpattern}(\\{vlnkain});{}$\6
${}\\{setpi}(\\{tblankscr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{tblanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{prefixes},\39\.{ANY});{}$\6
${}\\{setpi}(\\{charset},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{nochar},\39\.{ONE\_NOT}){}$;\par
\fi

\M{35}\B\X27:Inicializace prom�nn�ch p�i startu funkce \PB{\\{tie}}\X${}%
\mathrel+\E{}$\6
$\\{listpatt}\K\\{normallist}\K\\{vlnkalist}{}$;\par
\fi

\M{36}\B\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{vlnkain}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{char} \|p;\7
${}\\{ind}\MM;{}$\6
${}\|p\K\\{buff}[\\{ind}\MM];{}$\6
\&{while} ${}(\\{strchr}(\\{blanks},\39\\{buff}[\\{ind}])\I\NULL){}$\1\5
${}\\{ind}\MM;{}$\2\6
${}\\{ind}\PP;{}$\6
${}\\{buff}[\\{ind}\PP]\K\.{'\~'};{}$\6
${}\\{buff}[\\{ind}\PP]\K\|p;{}$\6
${}\\{numchanges}\PP;{}$\6
\4${}\}{}$\2\par
\fi

\M{37}Podobn� pro tvorbu vlnky \uv{p�es ��dek} vytvo��me pattern a k�d
procedury.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
\\{setpattern}(\\{vlnkacr});\6
${}\\{setpi}(\\{tblankscr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{tblanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{prefixes},\39\.{ANY});{}$\6
${}\\{setpi}(\\{charset},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{cr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{nochar},\39\.{ONE\_NOT}){}$;\par
\fi

\M{38}V procedu�e k tomuto patternu mus�me o�et�it p��pad typu
\uv{\.{a\char126v\char92np}},
kdy nelze prost� p�ehodit \uv{\.{\char92n}} za \uv{\.{v}}, proto�e
bychom roztrhli
mezeru sv�zanou vlnkou u� d��ve. Proto mus�me vyhledat vhodn� m�sto pro
roztr�en� ��dku, kter� bude a� {\it p�ed\/} znakem \uv{\.{a}}. P�i d�sledn�m
o�et�en� tohoto fenom�nu m��eme dokonce narazit na situaci
\uv{\.{\char92n\ v\char126v\char126v\char92np}},  kde nem��eme vlo�it
\uv{\.{\char92n}} p�ed prvn� v�skyt \uv{\.{v}}, proto�e bychom dostali
\uv{\.{\char92n\char92n}}, tedy pr�zdn�
��dek. Ten je v \TeX{}u interperetov�n odli�n�. V t�to v�jime�n�
situaci pouze zru��me st�vaj�c� (v po�ad� druh�) \uv{\.{\char92n}} a
nebudeme vytv��et nov�. Na v�stupu bude soubor o jeden ��dek krat��.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{vlnkacr}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{char} \|p;\6
\&{int} \|i${},{}$ \|j;\7
${}\\{ind}\MM;{}$\6
${}\|p\K\\{buff}[\\{ind}\MM];{}$\6
\&{while} ${}(\\{strchr}(\\{blankscr},\39\\{buff}[\\{ind}])\I\NULL){}$\1\5
${}\\{ind}\MM;{}$\2\6
${}\|i\K\\{ind}{}$;\C{ misto predlozky, kterou chceme vazat }\6
\&{while} ${}(\|i\G\T{0}\W(\\{strchr}(\\{blankscr},\39\\{buff}[\|i])\E%
\NULL)){}$\1\5
${}\|i\MM;{}$\2\6
${}\|j\K\|i;{}$\6
\&{while} ${}(\|i\G\T{0}\W(\\{strchr}(\\{blanks},\39\\{buff}[\|i])\I\NULL)){}$%
\1\5
${}\|i\MM;{}$\2\6
\&{if} ${}(\|i\G\T{0}\W\\{buff}[\|i]\E\.{'\\n'}){}$\1\5
${}\|j\K{-}\T{1};{}$\2\6
\&{if} ${}(\|j\G\T{0}){}$\1\5
${}\\{buff}[\|j]\K\.{'\\n'};{}$\2\6
\&{else}\1\5
${}\\{numline}\MM;{}$\2\6
${}\\{ind}\PP;{}$\6
${}\\{buff}[\\{ind}\PP]\K\.{'\~'};{}$\6
${}\\{buff}[\\{ind}\PP]\K\|p;{}$\6
${}\\{numchanges}\PP;{}$\6
\4${}\}{}$\2\par
\fi

\M{39}Nyn� vytvo��me patterny pro p��pady typu \.{\char92uv\char`\{v lese\char`%
\}}.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
\\{setpattern}(\\{vlnkain});\C{ na radku }\6
${}\\{setpi}(\\{tblankscr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{backslash},\39\.{ONE});{}$\6
${}\\{setpi}(\\{letters},\39\.{ONE});{}$\6
${}\\{setpi}(\\{letters},\39\.{ANY});{}$\6
${}\\{setpi}(\\{openbrace},\39\.{ONE});{}$\6
${}\\{setpi}(\\{prefixes},\39\.{ANY});{}$\6
${}\\{setpi}(\\{charset},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{nochar},\39\.{ONE\_NOT});{}$\6
\\{setpattern}(\\{vlnkacr});\C{ pres radek }\6
${}\\{setpi}(\\{tblankscr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{backslash},\39\.{ONE});{}$\6
${}\\{setpi}(\\{letters},\39\.{ONE});{}$\6
${}\\{setpi}(\\{letters},\39\.{ANY});{}$\6
${}\\{setpi}(\\{openbrace},\39\.{ONE});{}$\6
${}\\{setpi}(\\{prefixes},\39\.{ANY});{}$\6
${}\\{setpi}(\\{charset},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{cr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{nochar},\39\.{ONE\_NOT}){}$;\par
\fi

\M{40}Vytvo��me patterny a proceduru pro potlat�en� tvorby vlnky u p�smen t�sn�
n�sleduj�c�ch sekvence \.{\char92TeX} a \.{\char92LaTeX}. Tj. nechceme, aby
nap� z textu \uv{\.{Vlastnosti~\char92TeX~u~jsou...}} jsme dostali text
s nespr�vn� v�zan�m p�smenem
\uv{\.{Vlastnosti~\char92TeX~u\char126jsou...}}.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
$\\{normalpattern}(\\{tielock},\39\.{"\\\\TeX"});{}$\6
${}\\{setpi}(\\{blankscr},\39\.{ONE});{}$\6
${}\\{normalpattern}(\\{tielock},\39\.{"\\\\LaTeX"});{}$\6
${}\\{setpi}(\\{blankscr},\39\.{ONE}){}$;\par
\fi

\M{41}Procedura \PB{\\{tielock}} obsahuje ne�ist� trik. P�i prov�d�n� procedury
je
pr�v� na�ten znak z \PB{\\{blankscr}} a je ulo�en do buff. Testy na otev�r�n�
nov�ch pattern� pro tento znak teprve budou n�sledovat a testuj� se na
hodnotu prom�nn� \PB{\|c}. Sta�� tedy zm�nit hodnotu \PB{\|c} a vlnkovac�
patterny se
neotev�ou.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{tielock}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
${}\|c\K\T{1};{}$\6
\4${}\}{}$\2\par
\fi

\M{42}O�et��me nyn� p�echod do/z matematick�ho re�imu \TeX{}u. Uvnit� math
m�du vlnky ned�l�me. P�i zji�t�n�m nesouladu v p�echodech mezi
math-m�dy spust�me n�sleduj�c� proceduru.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{printwarning}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\R\\{silent}){}$\1\5
${}\\{fprintf}(\\{stderr},\39\.{"\~!\~\ warning:\ text/m}\)\.{ath/verb\ mode\
mismat}\)\.{ch,\ \ file:\ \%s,\ \ line}\)\.{:\ \%ld\\n"},\39\\{filename},\39%
\\{numline}-(\|c\E\.{'\\n'}\?\T{1}:\T{0}));{}$\2\6
${}\\{status}\K\.{WARNING};{}$\6
\4${}\}{}$\2\par
\fi

\M{43}Za�neme patterny pro p�echod do/z matematick�ho re�imu, ohrani�en�ho
jedn�m dolarem, nebo v La\TeX{}u p��slu�n�mi sekvencemi.  Sekvence
La\TeX{}u \.{\char92(} a \.{\char92)} nejsou zahrnuty, proto�e b�vaj�
�asto p�edefinov�ny k jin�m u�ite�n�j��m v�cem.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
\&{if} ${}(\R\\{nomath}){}$\5
${}\{{}$\1\6
${}\\{mathlist}\K\\{setpattern}(\\{onedollar});{}$\6
${}\\{setpi}(\\{dolar},\39\.{ONE});{}$\6
${}\\{setpi}(\\{dolar},\39\.{ONE\_NOT});{}$\6
\&{if} (\\{latex})\5
${}\{{}$\1\6
${}\\{normalpattern}(\\{mathin},\39\.{"\\\\begin.\{math\}"});{}$\6
${}\\{normalpattern}(\\{mathout},\39\.{"\\\\end.\{math\}"});{}$\6
\4${}\}{}$\2\6
\4${}\}{}$\2\par
\fi

\M{44}\B\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{mathin}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{mode}\I\.{TEXTMODE}){}$\1\5
\\{printwarning}(\,);\2\6
${}\\{mode}\K\.{MATHMODE};{}$\6
${}\\{normallist}\K\\{listpatt}\K\\{mathlist};{}$\6
\4${}\}{}$\2\7
\&{static} \&{void} \\{mathout}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{mode}\I\.{MATHMODE}){}$\1\5
\\{printwarning}(\,);\2\6
${}\\{mode}\K\.{TEXTMODE};{}$\6
${}\\{normallist}\K\\{listpatt}\K\\{vlnkalist};{}$\6
\4${}\}{}$\2\par
\fi

\M{45}P�i programov�n� procedury \PB{\\{onedollar}} nesm�me zapomenout na
v�skyt
sekvence \.{\char92\$}. V tom p��pad� akci ignorujeme. Podobn� u sekvence
\.{\$\$} souhlas� ten druh� dolar s na��m patternem, ale to u� jsme uvnit�
display m�du. V takov�m p��pad� tak� nic ned�l�me.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{onedollar}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{buff}[\\{ind}-\T{3}]\E\.{'\\\\'}\V(\\{buff}[\\{ind}-\T{3}]\E\.{'%
\$'}\W\\{buff}[\\{ind}-\T{4}]\I\.{'\\\\'})){}$\1\5
\&{return};\2\6
\&{if} ${}(\\{mode}\E\.{DISPLAYMODE}){}$\1\5
\\{printwarning}(\,);\2\6
\&{else}\5
${}\{{}$\1\6
\&{if} ${}(\\{mode}\E\.{TEXTMODE}){}$\1\5
\\{mathin}(\,);\2\6
\&{else}\1\5
\\{mathout}(\,);\2\6
\4${}\}{}$\2\6
\4${}\}{}$\2\par
\fi

\M{46}Pokud najdeme pr�zdn� ��dek, p�ekontrolujeme, zda n�hodou nejsme v
math-m�du. Pokud ano, vyp��eme varov�n� a p�ejdeme do textov�ho m�du.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
$\\{parcheck}\K\\{setpattern}(\\{checkmode});{}$\6
${}\\{setpi}(\\{cr},\39\.{ONE});{}$\6
${}\\{setpi}(\\{blanks},\39\.{ANY});{}$\6
${}\\{setpi}(\\{cr},\39\.{ONE}){}$;\par
\fi

\M{47}\B\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{checkmode}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{mode}\I\.{TEXTMODE}){}$\5
${}\{{}$\1\6
\\{printwarning}(\,);\6
${}\\{mode}\K\.{TEXTMODE};{}$\6
${}\\{normallist}\K\\{listpatt}\K\\{vlnkalist};{}$\6
\4${}\}{}$\2\6
\4${}\}{}$\2\par
\fi

\M{48}Nyn� o�et��me v�skyt dvou dolar�, tj. vstup do/z display m�du.
Rovn� mysleme na La\TeX{}isty a jejich prost�ed� pro display-m�d. Proto�e
je mo�n� alternativa s hv�zdi�kou na konci n�zvu prost�ed�, rad�ji u�
uzav�rac� z�vorku do patternu nezahrnujeme.

\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
\&{if} ${}(\R\\{nomath}){}$\5
${}\{{}$\1\6
${}\\{normalpattern}(\\{twodollars},\39\.{"\$\$"});{}$\6
\&{if} (\\{latex})\5
${}\{{}$\1\6
${}\\{normalpattern}(\\{displayin},\39\.{"\\\\begin.\{displaymat}\)\.{h"});{}$\6
${}\\{normalpattern}(\\{displayin},\39\.{"\\\\begin.\{equation"});{}$\6
${}\\{normalpattern}(\\{displayout},\39\.{"\\\\end.\{displaymath"});{}$\6
${}\\{normalpattern}(\\{displayout},\39\.{"\\\\end.\{equation"});{}$\6
\4${}\}{}$\2\6
\4${}\}{}$\2\par
\fi

\M{49}\B\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{displayin}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{mode}\I\.{TEXTMODE}){}$\1\5
\\{printwarning}(\,);\2\6
${}\\{mode}\K\.{DISPLAYMODE};{}$\6
${}\\{normallist}\K\\{listpatt}\K\\{parcheck};{}$\6
\4${}\}{}$\2\7
\&{static} \&{void} \\{displayout}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{mode}\I\.{DISPLAYMODE}){}$\1\5
\\{printwarning}(\,);\2\6
${}\\{mode}\K\.{TEXTMODE};{}$\6
${}\\{normallist}\K\\{listpatt}\K\\{vlnkalist};{}$\6
\4${}\}{}$\2\7
\&{static} \&{void} \\{twodollars}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{buff}[\\{ind}-\T{3}]\E\.{'\\\\'}){}$\1\5
\&{return};\2\6
\&{if} ${}(\\{mode}\E\.{DISPLAYMODE}){}$\1\5
\\{displayout}(\,);\2\6
\&{else}\1\5
\\{displayin}(\,);\2\6
\4${}\}{}$\2\par
\fi

\M{50}N�sleduje o�et�en� tzv. verbatim m�du. Pro plain i La\TeX{} jsou
nej�ast�j��
z�vorky pro verbatim mod tyto (variantu s \.{\char92begtt} pou��v�m
s oblibou j�).
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
\&{if} ${}(\R\\{noverb}){}$\5
${}\{{}$\1\6
${}\\{verblist}\K\\{normalpattern}(\\{verbinchar},\39\.{"\\\\verb"});{}$\6
${}\\{setpi}(\\{blankscr},\39\.{ANY});{}$\6
${}\\{setpi}(\\{blankscr},\39\.{ONE\_NOT});{}$\6
${}\\{normalpattern}(\\{verbin},\39\.{"\\\\begtt"});{}$\6
\&{if} (\\{latex})\1\5
${}\\{normalpattern}(\\{verbin},\39\.{"\\\\begin.\{verbatim"});{}$\2\6
\4${}\}{}$\2\6
\&{if} (\\{web})\5
${}\{{}$\1\6
${}\\{normalpattern}(\\{verbin},\39\.{"@<"});{}$\6
${}\\{normalpattern}(\\{verbin},\39\.{"@d"});{}$\6
\4${}\}{}$\2\6
\&{if} ${}(\R\\{noverb}){}$\5
${}\{{}$\1\6
${}\\{verboutlist}[\T{0}]\K\\{setpattern}(\\{verbout});{}$\6
${}\\{setpi}(\\{verbchar},\39\.{ONE});{}$\6
${}\\{verboutlist}[\T{1}]\K\\{normalpattern}(\\{verbout},\39\.{"%
\\\\endtt"});{}$\6
\&{if} (\\{latex})\1\5
${}\\{verboutlist}[\T{2}]\K\\{normalpattern}(\\{verbout},\39\.{"\\\\end%
\{verbatim"});{}$\2\6
\4${}\}{}$\2\6
\&{if} (\\{web})\5
${}\{{}$\1\6
${}\\{verboutlist}[\T{3}]\K\\{normalpattern}(\\{verbout},\39\.{"@\ "});{}$\6
${}\\{normalpattern}(\\{verbout},\39\.{"@*"});{}$\6
${}\\{normalpattern}(\\{verbout},\39\.{"@>|"});{}$\6
\4${}\}{}$\2\par
\fi

\M{51}Procedura \PB{\\{verbinchar}} se od \uv{spole�n�} procedury \PB{%
\\{verbin}} li�� v
tom, �e zavede do stringu \PB{\\{verbchar}} moment�ln� hodnotu prom�nn� \PB{%
\|c}.
Proto druh� v�skyt t�to hodnoty verbatim re�im ukon��.
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{int} \\{prevmode};\6
\&{PATTERN} ${}{*}\\{prevlist},{}$ ${}{*}\\{verboutlist}[\T{4}];{}$\6
\&{char} \\{verbchar}[\T{2}];\7
\&{static} \&{void} \\{verbinchar}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
${}\\{prevmode}\K\\{mode};{}$\6
${}\\{verbchar}[\T{0}]\K\|c;{}$\6
${}\|c\K\T{1};{}$\6
${}\\{listpatt}\K\\{normallist}\K\\{verboutlist}[\T{0}];{}$\6
${}\\{prevlist}\K\\{listpatt}\MG\\{next};{}$\6
${}\\{listpatt}\MG\\{next}\K\NULL;{}$\6
${}\\{mode}\K\.{VERBMODE};{}$\6
\4${}\}{}$\2\par
\fi

\M{52}P�i programov�n� \uv{obecn�} funkce \PB{\\{verbin}} mus�me db�t na to,
aby
z�stal aktivn� pouze odpov�daj�c� \uv{v�stupn�} pattern k dan�mu
vstupn�mu. Tak� si zapamatujeme m�d, ze kter�ho jsme do verbatim
oblasti vstoupili, abychom se k n�mu mohli vr�tit (nap�. uvnit�
math. m�du m��e b�t
\.{\char92hbox} a v n�m lok�ln� verbatim konstrukce).
\Y\B\4\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{verbin}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{int} \|i;\7
${}\|i\K\T{0};{}$\6
${}\\{prevmode}\K\\{mode};{}$\6
\&{switch} (\|c)\5
${}\{{}$\1\6
\4\&{case} \.{'t'}:\5
${}\|i\K\T{1};{}$\6
\&{break};\6
\4\&{case} \.{'m'}:\5
${}\|i\K\T{2};{}$\6
\&{break};\6
\4\&{case} \.{'<'}:\5
;\6
\4\&{case} \.{'d'}:\5
${}\|i\K\T{3};{}$\6
\&{if} ${}(\\{buff}[\\{ind}-\T{3}]\E\.{'@'}){}$\1\5
\&{return};\C{ dvojity @ ignorovat }\2\6
\&{break};\6
\4${}\}{}$\2\6
${}\\{listpatt}\K\\{normallist}\K\\{verboutlist}[\|i];{}$\6
${}\\{prevlist}\K\\{listpatt}\MG\\{next};{}$\6
\&{if} ${}(\|c\I\.{'<'}\W\|c\I\.{'d'}){}$\1\5
${}\\{listpatt}\MG\\{next}\K\NULL;{}$\2\6
${}\\{mode}\K\.{VERBMODE};{}$\6
\4${}\}{}$\2\par
\fi

\M{53}\B\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{verbout}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
\&{if} ${}(\\{mode}\I\.{VERBMODE}){}$\1\5
\&{return};\2\6
\&{if} ${}(\\{web}\W\\{buff}[\\{ind}-\T{2}]\E\.{'@'}\W\\{buff}[\\{ind}-\T{3}]\E%
\.{'@'}){}$\1\5
\&{return};\2\6
${}\\{mode}\K\\{prevmode};{}$\6
${}\\{normallist}\MG\\{next}\K\\{prevlist};{}$\6
\&{switch} (\\{mode})\5
${}\{{}$\1\6
\4\&{case} \.{DISPLAYMODE}:\5
${}\\{normallist}\K\\{listpatt}\K\\{parcheck};{}$\6
\&{break};\6
\4\&{case} \.{MATHMODE}:\5
${}\\{normallist}\K\\{listpatt}\K\\{mathlist};{}$\6
\&{break};\6
\4\&{case} \.{TEXTMODE}:\5
${}\\{normallist}\K\\{listpatt}\K\\{vlnkalist};{}$\6
\&{break};\6
\4${}\}{}$\2\6
\4${}\}{}$\2\par
\fi

\M{54}Nyn� implementujeme vlastnost d��ve pou��van�ho programu vlnka, tj. �e
lze jeho �innost vypnout a op�t zapnout v koment���ch. Vytv���me druh�
nez�visl� seznam pattern� a proto nejprve pronulujeme \PB{\\{lastpt}}.
\Y\B\4\X21:Inicializace datov�ch struktur\X${}\mathrel+\E{}$\6
$\\{lastpt}\K\T{0};{}$\6
${}\\{commentlist}\K\\{normalpattern}(\\{tieoff},\39\.{"\%.\~.-"});{}$\6
${}\\{normalpattern}(\\{tieon},\39\.{"\%.\~.+"}){}$;\par
\fi

\M{55}\B\X6:Pomocn� funkce\X${}\mathrel+\E{}$\6
\&{static} \&{void} \\{tieoff}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
${}\\{normallist}\K\NULL;{}$\6
\4${}\}{}$\2\7
\&{static} \&{void} \\{tieon}(\&{void})\1\1\2\2\6
${}\{{}$\1\6
${}\\{normallist}\K\\{vlnkalist};{}$\6
\4${}\}{}$\2\par
\fi

\M{56}Dal�� pl�novan� vylep�en�. Program by mohl ��st definici sv�ho chov�n�
nejen z~p��kazov� ��dky, ale v~mnohem kompletn�j�� podob�, v�etn�
u�ivatelsky definovan�ch pattern�, z koment��ov� oblasti ve �ten�m souboru.
Parametry zde uveden� by mohly m�t vy��� prioritu, ne� parametry
z~p��kazov� ��dky a mohl by se t�eba roz�i�ovat seznam sekvenc�, za nimi�
p�smena nemaj� b�t v�zana vlnkou (zat�m je implemenov�no na pevno jen
\.{\char92TeX} a \.{\char92LaTeX}).

\fi

\N{1}{57}Rejst��k.


\fi


\inx
\fin
\con
