OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [libgui/] [src/] [subcommand.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* subcommand.c - Automate handling of subcommands in Tcl.
2
   Copyright (C) 1997 Cygnus Solutions.
3
   Written by Tom Tromey <tromey@cygnus.com>.  */
4
 
5
#include <string.h>
6
 
7
#include <tcl.h>
8
 
9
#include "subcommand.h"
10
 
11
/* A pointer to this structure is the clientdata for
12
   subcommand_implementation.  */
13
 
14
struct subcommand_clientdata
15
{
16
  const struct ide_subcommand_table *commands;
17
  ClientData subdata;
18
  Tcl_CmdDeleteProc *delete;
19
};
20
 
21
/* This is called when one of our commands is deleted.  */
22
static void
23
subcommand_deleted (ClientData cd)
24
{
25
  struct subcommand_clientdata *data = (struct subcommand_clientdata *) cd;
26
 
27
  if (data->delete)
28
    (*data->delete) (data->subdata);
29
  Tcl_Free ((char *) data);
30
}
31
 
32
/* This function implements any Tcl command registered as having
33
   subcommands.  The ClientData here must be a pointer to the command
34
   table.  */
35
static int
36
subcommand_implementation (ClientData cd, Tcl_Interp *interp,
37
                           int argc, char *argv[])
38
{
39
  struct subcommand_clientdata *data = (struct subcommand_clientdata *) cd;
40
  const struct ide_subcommand_table *commands = data->commands;
41
  int i;
42
 
43
  if (argc < 2)
44
    {
45
      Tcl_AppendResult (interp, "wrong # args: must be \"",
46
                        argv[0], " key ?arg ...?\"", (char *) NULL);
47
      return (TCL_ERROR);
48
    }
49
 
50
  for (i = 0; commands[i].method != NULL; ++i)
51
    {
52
      if (! strcmp (argv[1], commands[i].method))
53
        {
54
          if (argc < commands[i].min_args)
55
            {
56
              char buf[20];
57
              Tcl_AppendResult (interp, "wrong # args: got ", (char *) NULL);
58
              sprintf (buf, "%d", argc);
59
              Tcl_AppendResult (interp, buf, " but expected at least ",
60
                                (char *) NULL);
61
              sprintf (buf, "%d", commands[i].min_args);
62
              Tcl_AppendResult (interp, buf, (char *) NULL);
63
              return (TCL_ERROR);
64
            }
65
 
66
          if (commands[i].max_args > 0 && argc > commands[i].max_args)
67
            {
68
              char buf[20];
69
              Tcl_AppendResult (interp, "wrong # args: got ", (char *) NULL);
70
              sprintf (buf, "%d", argc);
71
              Tcl_AppendResult (interp, buf, " but expected at most ",
72
                                (char *) NULL);
73
              sprintf (buf, "%d", commands[i].max_args);
74
              Tcl_AppendResult (interp, buf, (char *) NULL);
75
              return (TCL_ERROR);
76
            }
77
 
78
          return (commands[i].func (data->subdata, interp, argc, argv));
79
        }
80
    }
81
 
82
  Tcl_AppendResult (interp, "unrecognized key \"", argv[1],
83
                    "\"; must be one of ", (char *) NULL);
84
  for (i = 0; commands[i].method != NULL; ++i)
85
    Tcl_AppendResult (interp, "\"", commands[i].method,
86
                      (commands[i + 1].method == NULL) ? "\"" : "\", ",
87
                      (char *) NULL);
88
  return (TCL_ERROR);
89
}
90
 
91
/* Define a command with subcommands.  */
92
int
93
ide_create_command_with_subcommands (Tcl_Interp *interp, char *name,
94
                                     const struct ide_subcommand_table *table,
95
                                     ClientData subdata,
96
                                     Tcl_CmdDeleteProc *delete)
97
{
98
  int i;
99
  struct subcommand_clientdata *data;
100
 
101
  /* Sanity check.  */
102
  for (i = 0; table[i].method != NULL; ++i)
103
    {
104
      if ((table[i].min_args > table[i].max_args && table[i].max_args != -1)
105
          || table[i].min_args < 2
106
          || table[i].max_args < -1)
107
        {
108
          Tcl_AppendResult (interp, "subcommand \"", table[i].method,
109
                            "\" of command \"", name,
110
                            "\" has bad argument count",
111
                            (char *) NULL);
112
          return (TCL_ERROR);
113
        }
114
    }
115
 
116
  data = (struct subcommand_clientdata *) Tcl_Alloc (sizeof *data);
117
  data->commands = table;
118
  data->subdata = subdata;
119
  data->delete = delete;
120
 
121
  if (Tcl_CreateCommand (interp, name, subcommand_implementation,
122
                         (ClientData) data, subcommand_deleted) == NULL)
123
    return (TCL_ERROR);
124
 
125
  return (TCL_OK);
126
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.