File: system_variable.c

package info (click to toggle)
fuse-emulator 1.3.2%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 10,456 kB
  • ctags: 9,261
  • sloc: ansic: 83,215; sh: 11,503; perl: 3,806; makefile: 1,000; yacc: 254; lex: 143
file content (158 lines) | stat: -rw-r--r-- 4,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* event.c: Debugger system variables
   Copyright (c) 2016 Philip Kendall

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <string.h>

#ifdef HAVE_LIB_GLIB
#include <glib.h>
#endif				/* #ifdef HAVE_LIB_GLIB */

#include <libspectrum.h>

#include "debugger_internals.h"
#include "fuse.h"
#include "ui/ui.h"
#include "utils.h"

typedef struct system_variable_t {
  char *type;
  char *detail;
  debugger_get_system_variable_fn_t get;
  debugger_set_system_variable_fn_t set;
} system_variable_t;

static GArray *system_variables;

void
debugger_system_variable_init( void )
{
  system_variables = g_array_new( FALSE, FALSE, sizeof( system_variable_t ) );
}

void
debugger_system_variable_register( const char *type, const char *detail,
                                   debugger_get_system_variable_fn_t get,
                                   debugger_set_system_variable_fn_t set )
{
  system_variable_t sysvar;

  sysvar.type = utils_safe_strdup( type );
  sysvar.detail = utils_safe_strdup( detail );
  sysvar.get = get;
  sysvar.set = set;

  g_array_append_val( system_variables, sysvar );
}

static int
system_variable_matches( system_variable_t *sysvar, const char *type, const char *detail )
{
  return strcasecmp( type, sysvar->type ) == 0 &&
         strcasecmp( detail, sysvar->detail ) == 0;
}

static int
find_system_variable( const char *type, const char *detail, system_variable_t *out )
{
  size_t i;

  for( i = 0; i < system_variables->len; i++ ) {
    system_variable_t sysvar =
      g_array_index( system_variables, system_variable_t, i );

    if( system_variable_matches( &sysvar, type, detail ) ) {
      if( out != NULL ) *out = sysvar;
      return i;
    }
  }

  return -1;
}

int
debugger_system_variable_find( const char *type, const char *detail )
{
  return find_system_variable( type, detail, NULL );
}

libspectrum_dword
debugger_system_variable_get( int system_variable )
{
  system_variable_t sysvar =
    g_array_index( system_variables, system_variable_t, system_variable );

  return sysvar.get();
}

void
debugger_system_variable_set( const char *type, const char *detail,
                              libspectrum_dword value )
{
  int index;
  system_variable_t sysvar;

  index = find_system_variable( type, detail, &sysvar );
  if( index == -1 ) {
    ui_error( UI_ERROR_ERROR, "Unknown system variable %s:%s", type, detail );
    return;
  }

  if (sysvar.set == NULL) {
    ui_error( UI_ERROR_ERROR, "System variable %s:%s cannot be set", type,
              detail );
    return;
  }

  sysvar.set( value );
}

void
debugger_system_variable_text( char *buffer, size_t length,
                               int system_variable )
{
  system_variable_t sysvar =
    g_array_index( system_variables, system_variable_t, system_variable );

  snprintf( buffer, length, "%s:%s", sysvar.type, sysvar.detail );
}

/* Tidy-up function called at end of emulation */
void
debugger_system_variable_end( void )
{
  int i;
  system_variable_t sysvar;

  if( !system_variables ) return;

  for( i = 0; i < system_variables->len; i++ ) {
    sysvar = g_array_index( system_variables, system_variable_t, i );
    libspectrum_free( sysvar.detail );
    libspectrum_free( sysvar.type );
  }

  g_array_free( system_variables, TRUE );
  system_variables = NULL;
}