~ubuntu-branches/ubuntu/precise/pcb/precise

« back to all changes in this revision

Viewing changes to src/command.c

  • Committer: Bazaar Package Importer
  • Author(s): Hamish Moffatt
  • Date: 2005-02-20 13:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050220131400-pfz66g5vhx0azl8f
Tags: 1.99j+20050127-2
* Improved package description: (closes: #295405)
* Fixed dependency: tk84 -> tk8.4 (closes: #295404)
* Updated README.debian (closes: #269578)
* Applied patch to src/djopt.c to allow compilation with gcc-4.0
  (closes: #294319), thanks to Andreas Jochens for the patch.
* Prevent example files from being compressed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: command.c,v 1.4 2004/08/30 02:52:04 danmc Exp $ */
 
2
 
1
3
/*
2
4
 *                            COPYRIGHT
3
5
 *
24
26
 *
25
27
 */
26
28
 
27
 
static  char    *rcsid = "$Id: command.c,v 1.6 1998/02/24 22:54:47 cad Exp $";
28
 
 
29
29
/* executes commands from user
30
30
 */
31
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
#include "config.h"
 
34
#endif
 
35
 
32
36
#include <stdlib.h>
 
37
#ifdef HAVE_STRING_H
33
38
#include <string.h>
 
39
#endif
34
40
#include <ctype.h>
35
41
 
36
42
#include "global.h"
37
 
 
 
43
#include "action.h"
38
44
#include "buffer.h"
39
45
#include "command.h"
40
46
#include "data.h"
42
48
#include "error.h"
43
49
#include "file.h"
44
50
#include "fileselect.h"
 
51
#include "gui.h"
45
52
#include "mymem.h"
46
53
#include "misc.h"
47
54
#include "rats.h"
48
55
#include "set.h"
49
56
 
 
57
#ifdef HAVE_LIBDMALLOC
 
58
#include <dmalloc.h>
 
59
#endif
 
60
 
 
61
RCSID("$Id: command.c,v 1.4 2004/08/30 02:52:04 danmc Exp $");
 
62
 
50
63
/* ---------------------------------------------------------------------------
51
64
 * some local types
52
65
 */
53
 
typedef int             (*FunctionTypePtr)(void);
 
66
typedef int (*FunctionTypePtr) (void);
54
67
 
55
68
typedef struct
56
69
{
57
 
        char                    *Text;                  /* commandstring */
58
 
        FunctionTypePtr Function;               /* function pointer */
59
 
} CommandType, *CommandTypePtr;
 
70
  char *Text;                   /* commandstring */
 
71
  FunctionTypePtr Function;     /* function pointer */
 
72
}
 
73
CommandType, *CommandTypePtr;
60
74
 
61
75
/* ---------------------------------------------------------------------------
62
76
 * local prototypes
63
77
 */
64
 
static  int             CommandCallAction(void);
65
 
static  int             CommandLoadElementToBuffer(void);
66
 
static  int             CommandLoadLayoutToBuffer(void);
67
 
static  int             CommandLoadLayout(void);
68
 
static  int             CommandLoadNetlist(void);
69
 
static  int             CommandSaveLayout(void);
70
 
static  int             CommandSaveLayoutAndQuit(void);
71
 
static  int             CommandQuit(void);
72
 
static  int             CommandHelp(void);
73
 
static  int             SetArgcArgv(char *);
 
78
static int CommandCallAction (void);
 
79
static int CommandLoadElementToBuffer (void);
 
80
static int CommandLoadLayoutToBuffer (void);
 
81
static int CommandLoadLayout (void);
 
82
static int CommandLoadNetlist (void);
 
83
static int CommandSaveLayout (void);
 
84
static int CommandSaveLayoutAndQuit (void);
 
85
static int CommandQuit (void);
 
86
static int CommandHelp (void);
 
87
static int SetArgcArgv (char *);
74
88
 
75
89
/* ---------------------------------------------------------------------------
76
90
 * some local identifiers
77
91
 */
78
 
static  CommandType     Command[] = {
79
 
        { "h",  CommandHelp },
80
 
        { "le", CommandLoadElementToBuffer },
81
 
        { "m",  CommandLoadLayoutToBuffer },
82
 
        { "rn", CommandLoadNetlist },
83
 
        { "s",  CommandSaveLayout },
84
 
        { "w",  CommandSaveLayout },
85
 
        { "wq", CommandSaveLayoutAndQuit },
86
 
        { "q",  CommandQuit },
87
 
        { "q!", CommandQuit },
88
 
        { "l",  CommandLoadLayout }};
 
92
static CommandType Command[] = {
 
93
  {"h", CommandHelp},
 
94
  {"le", CommandLoadElementToBuffer},
 
95
  {"m", CommandLoadLayoutToBuffer},
 
96
  {"rn", CommandLoadNetlist},
 
97
  {"s", CommandSaveLayout},
 
98
  {"w", CommandSaveLayout},
 
99
  {"wq", CommandSaveLayoutAndQuit},
 
100
  {"q", CommandQuit},
 
101
  {"q!", CommandQuit},
 
102
  {"l", CommandLoadLayout}
 
103
};
89
104
 
90
 
static  char    **argv;                                 /* command-line of arguments */
91
 
static  int             argc;
 
105
static char **argv;             /* command-line of arguments */
 
106
static int argc;
92
107
 
93
108
/* ---------------------------------------------------------------------------
94
109
 * quits the application after confirming
95
110
 * syntax: q
96
111
 */
97
 
static int CommandQuit(void)
 
112
static int
 
113
CommandQuit (void)
98
114
{
99
 
        if (argc != 1)
100
 
        {
101
 
                Message("Usage: q\n  quits the application\n"
102
 
                        "       q!\n"
103
 
                        "quits without saving or warning for its need\n");
104
 
                return(1);
105
 
        }
106
 
        if (strcmp(argv[0], "q!") == 0)
107
 
                QuitApplication();
108
 
        if (!PCB->Changed || ConfirmDialog("OK to lose data ?"))
109
 
                QuitApplication();
 
115
  if (argc != 1)
 
116
    {
 
117
      Message ("Usage: q\n  quits the application\n"
 
118
               "       q!\n"
 
119
               "quits without saving or warning for its need\n");
 
120
      return (1);
 
121
    }
 
122
  if (strcasecmp (argv[0], "q!") == 0)
 
123
    QuitApplication ();
 
124
  if (!PCB->Changed || ConfirmDialog ("OK to lose data ?"))
 
125
    QuitApplication ();
110
126
 
111
 
        return(0);      
 
127
  return (0);
112
128
}
113
129
 
114
130
/* ---------------------------------------------------------------------------
115
131
 * loads an element into the current buffer
116
132
 * syntax: le [name]
117
133
 */
118
 
static int CommandLoadElementToBuffer(void)
 
134
static int
 
135
CommandLoadElementToBuffer (void)
119
136
{
120
 
        char    *filename;
 
137
  char *filename;
121
138
 
122
 
        switch(argc)
123
 
        {
124
 
                case 1:         /* open fileselect box */
125
 
                        filename = FileSelectBox("load element to buffer:", NULL,
 
139
  switch (argc)
 
140
    {
 
141
    case 1:                     /* open fileselect box */
 
142
      filename = FileSelectBox ("load element to buffer:", NULL,
126
143
                                Settings.ElementPath);
127
 
                        if (filename && LoadElementToBuffer(PASTEBUFFER, filename, True))
128
 
                                SetMode(PASTEBUFFER_MODE);
129
 
                        break;
130
 
                        
131
 
                case 2:         /* filename is passed in commandline */
132
 
                        filename = argv[1];
133
 
                        if (filename && LoadElementToBuffer(PASTEBUFFER, filename, True))
134
 
                                SetMode(PASTEBUFFER_MODE);
135
 
                        break;
136
 
 
137
 
                default:        /* usage */
138
 
                        Message(False,"Usage: le [name]\n  loads element data to buffer\n");
139
 
                        return(1);
140
 
        }
141
 
        return(0);
 
144
      if (filename && LoadElementToBuffer (PASTEBUFFER, filename, True))
 
145
        SetMode (PASTEBUFFER_MODE);
 
146
      break;
 
147
 
 
148
    case 2:                     /* filename is passed in commandline */
 
149
      filename = argv[1];
 
150
      if (filename && LoadElementToBuffer (PASTEBUFFER, filename, True))
 
151
        SetMode (PASTEBUFFER_MODE);
 
152
      break;
 
153
 
 
154
    default:                    /* usage */
 
155
      Message (False, "Usage: le [name]\n  loads element data to buffer\n");
 
156
      return (1);
 
157
    }
 
158
  return (0);
142
159
}
143
160
 
144
161
/* ---------------------------------------------------------------------------
145
162
 * loads a layout into the current buffer
146
163
 * syntax: m [name]
147
164
 */
148
 
static int CommandLoadLayoutToBuffer(void)
 
165
static int
 
166
CommandLoadLayoutToBuffer (void)
149
167
{
150
 
        char    *filename;
 
168
  char *filename;
151
169
 
152
 
        switch(argc)
153
 
        {
154
 
                case 1:         /* open fileselect box */
155
 
                        filename = FileSelectBox("load layout to buffer:", NULL,
 
170
  switch (argc)
 
171
    {
 
172
    case 1:                     /* open fileselect box */
 
173
      filename = FileSelectBox ("load layout to buffer:", NULL,
156
174
                                Settings.FilePath);
157
 
                        if (filename && LoadLayoutToBuffer(PASTEBUFFER, filename))
158
 
                                SetMode(PASTEBUFFER_MODE);
159
 
                        break;
160
 
                        
161
 
                case 2:         /* filename is passed in commandline */
162
 
                        filename = argv[1];
163
 
                        if (filename && LoadLayoutToBuffer(PASTEBUFFER, filename))
164
 
                                SetMode(PASTEBUFFER_MODE);
165
 
                        break;
166
 
 
167
 
                default:        /* usage */
168
 
                        Message("Usage: m [name]\n  loads layout data to buffer\n");
169
 
                        return(1);
170
 
        }
171
 
        return(0);
 
175
      if (filename && LoadLayoutToBuffer (PASTEBUFFER, filename))
 
176
        SetMode (PASTEBUFFER_MODE);
 
177
      break;
 
178
 
 
179
    case 2:                     /* filename is passed in commandline */
 
180
      filename = argv[1];
 
181
      if (filename && LoadLayoutToBuffer (PASTEBUFFER, filename))
 
182
        SetMode (PASTEBUFFER_MODE);
 
183
      break;
 
184
 
 
185
    default:                    /* usage */
 
186
      Message ("Usage: m [name]\n  loads layout data to buffer\n");
 
187
      return (1);
 
188
    }
 
189
  return (0);
172
190
}
173
191
 
174
192
/* ---------------------------------------------------------------------------
175
193
 * saves the layout data and quits
176
194
 */
177
 
static int CommandSaveLayoutAndQuit(void)
 
195
static int
 
196
CommandSaveLayoutAndQuit (void)
178
197
{
179
 
        if (argc != 1 && argc != 2)
180
 
        {
181
 
                Message("Usage: wq [name]\n  saves layout data and quits\n");
182
 
                return(1);
183
 
        }
184
 
        if (!CommandSaveLayout())
185
 
        {
186
 
                if (!PCB->Changed || ConfirmDialog("OK to lose data ?"))
187
 
                        QuitApplication();
188
 
                return(0);
189
 
        }
190
 
        return(1);
 
198
  if (argc != 1 && argc != 2)
 
199
    {
 
200
      Message ("Usage: wq [name]\n  saves layout data and quits\n");
 
201
      return (1);
 
202
    }
 
203
  if (!CommandSaveLayout ())
 
204
    {
 
205
      if (!PCB->Changed || ConfirmDialog ("OK to lose data ?"))
 
206
        QuitApplication ();
 
207
      return (0);
 
208
    }
 
209
  return (1);
191
210
}
192
211
 
193
212
/* ----------------------------------------------------------------------
194
213
 * saves layout data
195
214
 * syntax: l name
196
215
 */
197
 
static int CommandSaveLayout(void)
 
216
static int
 
217
CommandSaveLayout (void)
198
218
{
199
 
        char    *filename;
 
219
  char *filename;
200
220
 
201
 
        switch(argc)
 
221
  switch (argc)
 
222
    {
 
223
    case 1:                     /* query name if necessary */
 
224
      if (!PCB->Filename)
202
225
        {
203
 
                case 1:                 /* query name if necessary */
204
 
                        if (!PCB->Filename)
205
 
                        {
206
 
                                filename = FileSelectBox("save layout as:", NULL,
207
 
                                        Settings.FilePath);
208
 
                                if (filename)
209
 
                                        SavePCB(filename);
210
 
                        }
211
 
                        else
212
 
                                SavePCB(PCB->Filename);
213
 
                        break;
214
 
 
215
 
                case 2:
216
 
                        SavePCB(argv[1]);
217
 
                        break;
218
 
 
219
 
                default:
220
 
                        Message("Usage: s [name] | w [name]\n  saves layout data\n");
221
 
                        return(1);
 
226
          filename = FileSelectBox ("save layout as:", NULL,
 
227
                                    Settings.FilePath);
 
228
          if (filename)
 
229
            SavePCB (filename);
222
230
        }
223
 
        return(0);
 
231
      else
 
232
        SavePCB (PCB->Filename);
 
233
      break;
 
234
 
 
235
    case 2:
 
236
      SavePCB (argv[1]);
 
237
      break;
 
238
 
 
239
    default:
 
240
      Message ("Usage: s [name] | w [name]\n  saves layout data\n");
 
241
      return (1);
 
242
    }
 
243
  return (0);
224
244
}
225
245
 
226
246
/* ----------------------------------------------------------------------
227
247
 * loads layout data
228
248
 * syntax: l [name]
229
249
 */
230
 
static int CommandLoadLayout(void)
 
250
static int
 
251
CommandLoadLayout (void)
231
252
{
232
 
        char    *filename;
233
 
 
234
 
        switch(argc)
235
 
        {
236
 
                case 1:         /* open fileselect box */
237
 
                        filename = FileSelectBox("load file:", NULL, Settings.FilePath);
238
 
                        if (!filename)
239
 
                                return(0);
240
 
                        break;
241
 
                        
242
 
                case 2:         /* filename is passed in commandline */
243
 
                        filename = argv[1];
244
 
                        break;
245
 
 
246
 
                default:        /* usage */
247
 
                        Message("Usage: l [name]\n  loads layout data\n");
248
 
                        return(1);
249
 
        }
250
 
 
251
 
        if (!PCB->Changed || ConfirmDialog("OK to override layout data?"))
252
 
                LoadPCB(filename);
253
 
        return(0);
 
253
  char *filename;
 
254
 
 
255
  switch (argc)
 
256
    {
 
257
    case 1:                     /* open fileselect box */
 
258
      filename = FileSelectBox ("load file:", NULL, Settings.FilePath);
 
259
      if (!filename)
 
260
        return (0);
 
261
      break;
 
262
 
 
263
    case 2:                     /* filename is passed in commandline */
 
264
      filename = argv[1];
 
265
      break;
 
266
 
 
267
    default:                    /* usage */
 
268
      Message ("Usage: l [name]\n  loads layout data\n");
 
269
      return (1);
 
270
    }
 
271
 
 
272
  if (!PCB->Changed || ConfirmDialog ("OK to override layout data?"))
 
273
    LoadPCB (filename);
 
274
  return (0);
254
275
}
255
276
 
256
277
/* ----------------------------------------------------------------------
257
278
 * reads netlist 
258
279
 * syntax: rn [name]
259
280
 */
260
 
static int CommandLoadNetlist(void)
 
281
static int
 
282
CommandLoadNetlist (void)
261
283
{
262
 
        char    *filename;
263
 
 
264
 
        switch(argc)
265
 
        {
266
 
                case 1:         /* open fileselect box */
267
 
                        filename = FileSelectBox("load file:", NULL, Settings.FilePath);
268
 
                        if (!filename)
269
 
                                return(0);
270
 
                        break;
271
 
                        
272
 
                case 2:         /* filename is passed in commandline */
273
 
                        filename = argv[1];
274
 
                        break;
275
 
 
276
 
                default:        /* usage */
277
 
                        Message("Usage: rn [name]\n  reads in a netlist file\n");
278
 
                        return(1);
279
 
        }
280
 
        if (PCB->Netlistname)
281
 
                SaveFree(PCB->Netlistname);
282
 
        PCB->Netlistname = StripWhiteSpaceAndDup(filename);
283
 
        return(0);
 
284
  char *filename;
 
285
 
 
286
  switch (argc)
 
287
    {
 
288
    case 1:                     /* open fileselect box */
 
289
      filename = FileSelectBox ("load file:", NULL, Settings.FilePath);
 
290
      if (!filename)
 
291
        return (0);
 
292
      break;
 
293
 
 
294
    case 2:                     /* filename is passed in commandline */
 
295
      filename = argv[1];
 
296
      break;
 
297
 
 
298
    default:                    /* usage */
 
299
      Message ("Usage: rn [name]\n  reads in a netlist file\n");
 
300
      return (1);
 
301
    }
 
302
  if (PCB->Netlistname)
 
303
    SaveFree (PCB->Netlistname);
 
304
  PCB->Netlistname = StripWhiteSpaceAndDup (filename);
 
305
  return (0);
284
306
}
285
307
 
286
308
/* ----------------------------------------------------------------------
287
309
 * Send an action command
288
310
 */
289
 
static int CommandCallAction(void)
 
311
static int
 
312
CommandCallAction (void)
290
313
{
291
 
        XtCallActionProc(Output.Porthole, argv[0], (XEvent *) NULL, &argv[1], argc - 1);
292
 
        return(0);
 
314
  CallActionProc (Output.Output, argv[0], (XEvent *) NULL, &argv[1],
 
315
                  argc - 1);
 
316
  return (0);
293
317
}
294
318
 
295
319
/* ----------------------------------------------------------------------
296
320
 * print a help message for commands
297
321
 */
298
 
static int CommandHelp()
 
322
static int
 
323
CommandHelp ()
299
324
{
300
 
        Message("following commands are supported:\n"
301
 
                "  Command()   execute an action command (too numerous to list)\n"
302
 
                "              see the manual for the list of action commands\n"
303
 
                "  h           display this help message\n"
304
 
                "  l  [file]   load layout\n"
305
 
                "  le [file]   load element to buffer\n"
306
 
                "  m  [file]   load layout to buffer (merge)\n"
307
 
                "  q           quits the application\n"
308
 
                "  q!          quits without save warning\n"
309
 
                "  rn [file]   read in a net-list file\n"
310
 
                "  s  [file]   save layout\n"
311
 
                "  w  [file]   save layout\n"
312
 
                "  wq [file]   save layout and quit\n");
313
 
        return(0);
 
325
  Message ("following commands are supported:\n"
 
326
           "  Command()   execute an action command (too numerous to list)\n"
 
327
           "              see the manual for the list of action commands\n"
 
328
           "  h           display this help message\n"
 
329
           "  l  [file]   load layout\n"
 
330
           "  le [file]   load element to buffer\n"
 
331
           "  m  [file]   load layout to buffer (merge)\n"
 
332
           "  q           quits the application\n"
 
333
           "  q!          quits without save warning\n"
 
334
           "  rn [file]   read in a net-list file\n"
 
335
           "  s  [file]   save layout\n"
 
336
           "  w  [file]   save layout\n"
 
337
           "  wq [file]   save layout and quit\n");
 
338
  return (0);
314
339
}
315
340
 
316
341
/* ----------------------------------------------------------------------
317
342
 * split commandline and fill argv/argc
318
343
 */
319
 
static int SetArgcArgv(char *Line)
 
344
static int
 
345
SetArgcArgv (char *Line)
320
346
{
321
 
        static  int             maxcount = 0;
322
 
                        char    *p;
 
347
  static int maxcount = 0;
 
348
  char *p;
323
349
 
324
 
        for(argc = 0, p = strtok(Line, " \t;,()"); p; p = strtok(NULL, " \t;,()"))
 
350
  for (argc = 0, p = strtok (Line, " \t;,()"); p;
 
351
       p = strtok (NULL, " \t;,()"))
 
352
    {
 
353
      /* allocate more memory */
 
354
      if (argc >= maxcount)
325
355
        {
326
 
                        /* allocate more memory */
327
 
                if (argc >= maxcount)
328
 
                {
329
 
                        maxcount += 20;
330
 
                        argv = (char **) MyRealloc(argv, maxcount*sizeof(char *),
331
 
                                "SetArgcArgv()");
332
 
                }
333
 
                argv[argc++] = p;
 
356
          maxcount += 20;
 
357
          argv = (char **) MyRealloc (argv, maxcount * sizeof (char *),
 
358
                                      "SetArgcArgv()");
334
359
        }
335
 
        return(argc);
 
360
      argv[argc++] = p;
 
361
    }
 
362
  return (argc);
336
363
}
337
364
 
338
365
/* ---------------------------------------------------------------------------
339
366
 * command is passed in, memory is already allocated
340
367
 */
341
 
void ExecuteUserCommand(char *CommandLine)
 
368
void
 
369
ExecuteUserCommand (char *CommandLine)
342
370
{
343
 
        int             i;
 
371
  int i;
344
372
 
345
 
        if (SetArgcArgv(CommandLine))
346
 
        {
347
 
                        /* scan command list */
348
 
                for (i = 0; i < ENTRIES(Command); i++)
349
 
                        if (!strcmp(Command[i].Text, argv[0]))
350
 
                                break;
351
 
                if (i == ENTRIES(Command))
352
 
                                /* it wasn't listed, it must have been an action command */
353
 
                        CommandCallAction();
354
 
                else
355
 
                        if (Command[i].Function())
356
 
                                XBell(Dpy, Settings.Volume);
357
 
        }
358
 
        else
359
 
                XBell(Dpy, Settings.Volume);
 
373
  if (SetArgcArgv (CommandLine))
 
374
    {
 
375
      /* scan command list */
 
376
      for (i = 0; i < ENTRIES (Command); i++)
 
377
        if (!strcasecmp (Command[i].Text, argv[0]))
 
378
          break;
 
379
      if (i == ENTRIES (Command))
 
380
        /* it wasn't listed, it must have been an action command */
 
381
        CommandCallAction ();
 
382
      else if (Command[i].Function ())
 
383
        Beep (Settings.Volume);
 
384
    }
 
385
  else
 
386
    Beep (Settings.Volume);
360
387
}
361