[pdftex] Experiment with a new TeX primitive

Hans Hagen pragma at wxs.nl
Wed Feb 4 17:40:21 CET 2004


At 12:03 31/01/2004, Nagy Bence wrote:
>Dear developers,
>
>I made a small experiment to understand better how web2c works
>(calling pascal<->C codes), so i defined a new TeX primitive
>(\experiment). It calls an external C-function to print out its
>parameter, and this function gives back a new string (like a search and
>replace engine).
>
>The code from the C-file:
>
>strnumber experiment(integer t)
>{
>     tex_printf("<%s>",makecstring(tokenstostring(t)));
>     return maketexstring("apple");
>}
>
>The main inserted code from pdftex.ch:
>
>@ @<Implement \.{\\experiment}@>=
>begin
>     call_func(scan_toks(false, true));
>     back_list(link(experiment(def_ref)));
>     delete_token_ref(def_ref);
>end
>
>All works fine except that I can not put back the new string ("apple")
>to the token list. The function back_list causes a segmentation
>fault, but it should work like the \lowercase or \uppercase primitive
>(the experiment C-function gives back an integer value well). What is
>wrong?

You need to simulate something:

\ifx\scratchread \undefined \newread \scratchread  \fi
\ifx\scratchwrite\undefined \newwrite\scratchwrite \fi

\let\endofresultline\relax
\let\resultofrun    \empty

\def\resultrunfile {\jobname.src.tmp}
\def\resultrunerror{no result}

\def\runsystemcommand#1%
   {\bgroup
    % reset results
    \global\let\endofresultline\relax
    \global\let\resultofrun\empty
    % erase resultfile
    \immediate\openout\scratchwrite\resultrunfile
    \immediate\write\scratchwrite{\resultrunerror}%
    \immediate\closeout\scratchwrite
    % run command
    \immediate\write18{#1 > \resultrunfile}%
    % define collector
    \def\runsystemcommand
      {\ifeof\scratchread \else
         \endlinechar -1
         \readline\scratchread to \next
         \xdef\resultofrun{\resultofrun\next\endofresultline}%
         \expandafter\runsystemcommand
       \fi}%
    % fetch result
    \openin\scratchread\resultrunfile\relax
    \runsystemcommand
    \closein\scratchread
    % done
    \egroup}

\starttext

\runsystemcommand{dir /s} \let\endofresultline\par \tt \resultofrun

\stoptext

so, the best place to look is in the etex code and see how the \readline 
results end up in tex

Hans  



More information about the pdftex mailing list