To: Tcl Overview, Windows, Tcl + C, Tk, Grid, Get Tcl, Graphical Applications


Créer votre propre interpréteur de commande Tcl en C.

Créer votre propre interpréteur Tcl est chose simple. Vous devrez programmer avec les librairies Tcl/Tk ainsi que les fichiers d'entetes de cette librairie , fichier include C ou C++. La méthode la plus simple est d'appeler Tcl_Main() dans votre fonction main(), comme il est montre dans le programme revcmd.c ci-dessous.

Le programme revcmd.c crée un nouvel interpréteur Tcl ecrit en C. Ce nouvel interpréteur aura une nouvelle commande appelé reverse du point de vue du langage Tcl.

L'appel à la fonction Tcl_CreateCommand associe le nom de la commande "reverse", avec la fonction C RevCommand. ( Notez que ce programme utilise la vieille méthode sur les String par rapport a l'approche objet introduit par Tcl 8.0. La methodlogie objet demande l'écriture d'un code plus complexe, mais donne des résultats plus performent, plus souple.

Le code si dessus provient du livre A HREF="tclbook.htm">Graphical Applications with Tcl and Tk.

/*
 revcmd.c 
 Tcl sample command to
 reverse two arguments.
*/

#include "stdio.h"
#include "stdlib.h"
#include "tcl.h"

/*
Implements a Tcl command named "reverse",
which reverses the first two arguments
and returns the reversed data.
*/
int RevCommand(
  ClientData client_data,
  Tcl_Interp* interp,
  int argc,
  char *argv[])

{    /* RevCommand */


    /* Reset result data. */
    Tcl_ResetResult(interp);

    /* Check number of arguments. */
    if (argc != 3) {
        Tcl_AppendResult(interp, 
            "ERROR: reverse requires two arguments.",
            (char*) NULL); /* Sentinel. */

        return TCL_ERROR;
    }

    /* Reverse arguments. */
    Tcl_AppendResult(interp,
        argv[2], " ", argv[1],
        (char*) NULL); /* Sentinel. */

    return TCL_OK;

}    /* RevCommand */


int Tcl_AppInit(Tcl_Interp* interp)
{
   int    status;

   status = Tcl_Init(interp);

   if (status != TCL_OK) {
       return TCL_ERROR;
   }

   /* Register your commands here... */
   Tcl_CreateCommand(interp,
    "reverse",
    (Tcl_CmdProc*) RevCommand,
    (ClientData) NULL,
    (Tcl_CmdDeleteProc*) NULL);


   return TCL_OK;
}

int
main(int argc, char** argv)
{
    Tcl_Main(argc, argv, Tcl_AppInit);
    return 0;
}

/* revcmd.c */

Pour obtenir un programme Tcl, vous devez linker avec la bibliothèque tcl et math.

Pour ajouter une commande Tk, vous pouvez utiliser comme exemple le code suivant.


#include "stdio.h"
#include "stdlib.h"
#include "tcl.h"
#include "tk.h"

int Tcl_AppInit(Tcl_Interp* interp)
{
   int    status;

   status = Tcl_Init(interp);

   if (status != TCL_OK)
   {
       return TCL_ERROR;
   }

   /* Initialize Tk values. */
   status = Tk_Init(interp);

   if (status != TCL_OK)
   {
       return TCL_ERROR;
   }

   /* Insert your code here... */

   return TCL_OK;
}

int main(int argc, char** argv)
{
    Tk_Main(argc, argv, Tcl_AppInit);
    return 0;
}

Avec les programmes Tk, vous auriez besoin de faire les liens avec les librairies Tk, Tcl et la librairie mathématique.

Bien sur, vos programmes seront plus complexe que le simple exemple donne ici.

26-Mar-98 Eric Foster-Johnson 22-Mar-99 traduction charles vidal

Tcl + C la page originale en anglais


Home Linux | Tcl | Perl | Motif | Japanese | My Books