~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to scipy/sandbox/xplt/src/gist/browser.c

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * BROWSER.C
 
3
 *
 
4
 * $Id: browser.c 685 2003-03-08 15:26:51Z travo $
 
5
 *
 
6
 * Main for GIST CGM viewer
 
7
 *
 
8
 */
 
9
/*    Copyright (c) 1994.  The Regents of the University of California.
 
10
                    All rights reserved.  */
 
11
 
 
12
#include <string.h>
 
13
#include "pstdlib.h"
 
14
#include "pstdio.h"
 
15
 
 
16
#include "xbasic.h"
 
17
#include "cgm.h"
 
18
 
 
19
/* OpenCGM, ReadCGM, CGMRelative, cgmLandscape, bg0fg1 in cgmin.h */
 
20
#include "cgmin.h"
 
21
/* GpEPSEngine declared in eps.h */
 
22
#include "eps.h"
 
23
 
 
24
extern long strtol(const char *, char **, int);
 
25
 
 
26
extern int cgmScaleToFit;
 
27
extern int gtDoEscapes;
 
28
 
 
29
/* List of commands in alphabetical order, 0 terminated */
 
30
static char *commandList[]= {
 
31
  "cgm", "display", "draw", "eps", "free", "help", "info", "open",
 
32
  "ps", "quit", "send", 0 };
 
33
 
 
34
/* Corresponding functions */
 
35
static int MakeCGM(int help);
 
36
static int MakeX(int help);
 
37
static int Draw(int help);
 
38
static int EPS(int help);
 
39
static int FreeAny(int help);
 
40
static int Help(int help);
 
41
static int Info(int help);
 
42
static int OpenIn(int help);
 
43
static int MakePS(int help);
 
44
static int Quit(int help);
 
45
static int Send(int help);
 
46
static int Special(int help);  /* must correspond to 0 in commandList */
 
47
static int nPrefix, cSuffix;   /* arguments for Special */
 
48
typedef int (*Command)(int help);
 
49
static Command CommandList[]= {
 
50
  &MakeCGM, &MakeX, &Draw, &EPS, &FreeAny, &Help, &Info, &OpenIn,
 
51
  &MakePS, &Quit, &Send, &Special };
 
52
static int GetCommand(char *token);
 
53
 
 
54
int amBatch= 0;
 
55
 
 
56
int warningCount= 0;
 
57
int no_warnings= 0;
 
58
 
 
59
static int CreateCGM(int device, char *name);
 
60
static int CreatePS(int device, char *name);
 
61
static int CreateX(int device, char *name);
 
62
 
 
63
static int GetPageGroup(char *token);
 
64
static int CheckEOL(char *command);
 
65
static void DrawSend(int ds, char *token);
 
66
static int GetDeviceList(int ds, char *command);
 
67
static int FindDevice(void);
 
68
static int SaveName(int device, char *name);
 
69
static void DoSpecial(int nPrefix, int cSuffix);
 
70
 
 
71
static int xPrefix= 0;
 
72
static void HandleExpose(Engine *engine, Drauing *drauing, int xy[]);
 
73
static void HandleOther(Engine *engine, int k, int md);
 
74
static int HelpAndExit(void);
 
75
static int MessageAndExit(char *msg);
 
76
 
 
77
extern int on_idle(void);
 
78
extern int on_quit(void);
 
79
extern void on_stdin(char *line);
 
80
static int did_startup = 0;
 
81
extern int do_startup(void);
 
82
 
 
83
int defaultDPI;
 
84
 
 
85
char *outNames[8];
 
86
int outLengths[8];
 
87
int outDraw[8], outSend[8], outMark[8];
 
88
 
 
89
int nPageGroups= 0;
 
90
int mPage[32], nPage[32], sPage[32];
 
91
int n_active_groups = 0;
 
92
 
 
93
static int nOut, noDisplay, x_only;
 
94
static char *inName;
 
95
 
 
96
int
 
97
on_launch(int argc, char *argv[])
 
98
{
 
99
  int i, j;
 
100
  char *arg;
 
101
 
 
102
  nOut= 0;
 
103
  for (i=0 ; i<8 ; i++) {
 
104
    outNames[i]= 0;
 
105
    outEngines[i]= 0;
 
106
    outDraw[i]= outSend[i]= outLengths[i]= 0;
 
107
  }
 
108
  noDisplay= amBatch= x_only= 0;
 
109
  defaultDPI= 100;
 
110
  inName= 0;
 
111
 
 
112
  p_quitter(&on_quit);
 
113
  p_idler(&on_idle);
 
114
  p_stdinit(&on_stdin);
 
115
  g_stdout = p_stdout;
 
116
 
 
117
  for (i=1 ; i<argc ; i++) {
 
118
    arg= argv[i];
 
119
 
 
120
    if (arg[0]=='-') {
 
121
      int fileType= -1;
 
122
      arg++;
 
123
      if (strcmp(arg, "cgm")==0) {
 
124
        fileType= 0;
 
125
        i++;
 
126
        if (i>=argc) return MessageAndExit("Missing file or display name");
 
127
        arg= argv[i];
 
128
      } else if (strcmp(arg, "ps")==0) {
 
129
        fileType= 1;
 
130
        i++;
 
131
        if (i>=argc) return MessageAndExit("Missing file or display name");
 
132
        arg= argv[i];
 
133
      } else if (strcmp(arg, "in")==0) {
 
134
        i++;
 
135
        if (i>=argc) return MessageAndExit("Missing file or display name");
 
136
        if (inName) return HelpAndExit();
 
137
        else inName= argv[i];
 
138
      } else if (strcmp(arg, "display")==0 || strcmp(arg, "d")==0) {
 
139
        fileType= 2;
 
140
        i++;
 
141
        if (i>=argc) return MessageAndExit("Missing file or display name");
 
142
        arg= argv[i];
 
143
      } else if (strcmp(arg, "f")==0) {
 
144
        amBatch= 1;
 
145
        fileType= 1;
 
146
        arg= "*stdout*";
 
147
      } else if (strcmp(arg, "nd")==0) noDisplay= 1;
 
148
      else if (strcmp(arg, "b")==0) amBatch= 1;
 
149
      else if (strcmp(arg, "nowarn")==0) no_warnings= 1;
 
150
      else if (strcmp(arg, "geometry")==0) {
 
151
        char *suffix;
 
152
        int w=0,h=0;
 
153
        i++;
 
154
        if (i>=argc) MessageAndExit("Missing geometry");
 
155
        arg = argv[i];
 
156
        w = (int)strtol(arg, &suffix, 10);
 
157
        if (suffix!=arg && *suffix=='x') {
 
158
          arg = suffix+1;
 
159
          h = (int)strtol(arg, &suffix, 10);
 
160
        }
 
161
        if (w < 10 || h < 10) MessageAndExit("Invalid geometry");
 
162
        gx75width = gx100width = w;
 
163
        gx75height = gx100height = h;
 
164
      } else if (strcmp(arg, "75")==0) defaultDPI= 75;
 
165
      else if (strcmp(arg, "100")==0) defaultDPI= 100;
 
166
      else if (strcmp(arg, "gks")==0) {
 
167
        gx75width= gx75height= 600;     /* 8x8 X window size */
 
168
        gx100width= gx100height= 800;   /* 8x8 X window size */
 
169
        cgmScaleToFit= 1;               /* 8x8 PostScript plotting area */
 
170
        gtDoEscapes= 0;
 
171
      } else if (strcmp(arg, "x")==0) x_only= 1;
 
172
      else if (strcmp(arg, "gks")==0) {
 
173
        cgmScaleToFit= 1;
 
174
        gtDoEscapes= 0;
 
175
      }
 
176
      else if (strcmp(arg, "fmbug")==0) epsFMbug= 1;
 
177
      else if (strcmp(arg, "bg0fg1")==0) bg0fg1= 1;
 
178
      else if (strcmp(arg, "esc0")==0) gtDoEscapes= 0;
 
179
      else if (strcmp(arg, "esc1")==0) gtDoEscapes= 1;
 
180
      else return HelpAndExit();
 
181
 
 
182
      if (fileType>=0) {
 
183
        if (nOut>=8)
 
184
          return MessageAndExit("At most 8 output files/displays allowed");
 
185
        for (j=0 ; j<nOut ; j++) if (strcmp(outNames[j], arg)==0)
 
186
          return MessageAndExit("Duplicate output filenames not allowed");
 
187
        outNames[nOut]= arg;
 
188
        outTypes[nOut]= fileType;
 
189
        nOut++;
 
190
      }
 
191
 
 
192
    } else if (arg[0]<='9' && arg[0]>='0') {
 
193
      if (GetPageGroup(arg)) return MessageAndExit("Try again");
 
194
 
 
195
    } else if (!inName) {
 
196
      inName= arg;
 
197
 
 
198
    } else {
 
199
      return HelpAndExit();
 
200
    }
 
201
  }
 
202
 
 
203
  if (inName && OpenCGM(inName)) inName= 0;
 
204
 
 
205
  if (amBatch) {
 
206
    if (!inName)
 
207
      return MessageAndExit("Must specify an input CGM file to use -b or -f");
 
208
    if (!nOut)
 
209
      return MessageAndExit("Must specify some output file to use -b");
 
210
    noDisplay= 1;
 
211
  }
 
212
 
 
213
  /* Create CGM and PostScript engines */
 
214
  for (i=0 ; i<nOut ; i++) {
 
215
    if (outTypes[i]==0) CreateCGM(i, outNames[i]);
 
216
    else if (outTypes[i]==1) CreatePS(i, outNames[i]);
 
217
  }
 
218
 
 
219
  /* If page list specified, do implied send command */
 
220
  if (amBatch && nPageGroups<=0) {
 
221
    mPage[0]= 1;
 
222
    nPage[0]= 32767;
 
223
    sPage[0]= 1;
 
224
    nPageGroups= 1;
 
225
  }
 
226
  if (nPageGroups>0) {
 
227
    for (i=0 ; i<8 ; i++) {
 
228
      if (!outSend[i]) GpDeactivate(outEngines[i]);
 
229
      if (outSend[i] && !GpActivate(outEngines[i])) n_active_groups++;
 
230
    }
 
231
  }
 
232
 
 
233
  g_initializer(&argc, argv);
 
234
 
 
235
  return 0;
 
236
}
 
237
 
 
238
int
 
239
do_startup(void)
 
240
{
 
241
  int i;
 
242
  did_startup = 1;
 
243
 
 
244
  if (!noDisplay) {
 
245
    int noX= 1;
 
246
    for (i=0 ; i<nOut ; i++) if (outTypes[i]==2) {
 
247
      if (!CreateX(i, outNames[i])) noX= 0;
 
248
    }
 
249
    if (noX && nOut<8) {
 
250
      if (!CreateX(nOut, 0)) {
 
251
        nOut++;
 
252
        noX= 0;
 
253
      }
 
254
    }
 
255
    noDisplay= noX;
 
256
  }
 
257
  if (noDisplay && x_only)
 
258
    return MessageAndExit("Must be an X display to use -x");
 
259
  if (x_only && !inName)
 
260
    return MessageAndExit("Must specify an input CGM file to use -x");
 
261
 
 
262
  if (n_active_groups)
 
263
    ReadCGM(mPage, nPage, sPage, nPageGroups);
 
264
  return 0;
 
265
}
 
266
 
 
267
static int need_prompt = 1;
 
268
 
 
269
int
 
270
on_idle(void)
 
271
{
 
272
  int flag;
 
273
  if (!did_startup && do_startup()) return 0;
 
274
  if (need_prompt && !x_only && !amBatch) p_stdout("gist> ");
 
275
  need_prompt = 0;
 
276
  flag = CatalogCGM();
 
277
  if (!flag && amBatch) p_quit();
 
278
  return flag;
 
279
}
 
280
 
 
281
int
 
282
on_quit(void)
 
283
{
 
284
  int i;
 
285
  for (i=0 ; i<8 ; i++) {
 
286
    if (outEngines[i]) {
 
287
      GpDeactivate(outEngines[i]);
 
288
      GpKillEngine(outEngines[i]);
 
289
    }
 
290
  }
 
291
 
 
292
  return 0;
 
293
}
 
294
 
 
295
/* ------------------------------------------------------------------------ */
 
296
 
 
297
static int HelpAndExit(void)
 
298
{
 
299
  p_stderr("Usage:   gist [cgminput] [options] [page list]\n\n");
 
300
  p_stderr("   options:  -in cgminput  (open cgminput for input)\n");
 
301
  p_stderr("             -cgm cgmout   (open cgmout for output)\n");
 
302
  p_stderr("             -ps psout     (open psout for output)\n");
 
303
  p_stderr("             -display host:n.m  (connect to X server)\n");
 
304
  p_stderr("             -75  (default to 75 dpi)\n");
 
305
  p_stderr("             -100 (default to 100 dpi)\n");
 
306
  p_stderr("             -gks (default to 8x8 inches)\n");
 
307
  p_stderr("             -geometry WxH (initial window size in pixels)\n");
 
308
  p_stderr("             -nd  (not even default X server)\n");
 
309
  p_stderr("             -b   (batch mode, implies -nd)\n");
 
310
  p_stderr("             -x   (graphics window only, no keyboard)\n");
 
311
  p_stderr("             -nowarn (only one warning message printed)\n");
 
312
  p_stderr("             -f   (PostScript to stdout, implies -b)\n");
 
313
  p_stderr("             -fmbug (if EPS files are for FrameMaker)\n");
 
314
  p_stderr("             -bg0fg1 (force color 0 to bg, 1 to fg)\n");
 
315
  p_stderr("             -esc0 (skip ^_! escapes-assumed if -gks)\n");
 
316
  p_stderr("             -esc1 (handle ^_! text escapes-default)\n");
 
317
  p_stderr("   page list:  [page group] [page group] ...\n");
 
318
  p_stderr("   page group:  n     (output page n)\n");
 
319
  p_stderr("   page group:  n-m   (output pages n->m inclusive)\n");
 
320
  p_stderr("   page group:  n-m-s (output every s-th page, n-m)\n");
 
321
  p_quit();
 
322
  return 1;
 
323
}
 
324
 
 
325
static int MessageAndExit(char *msg)
 
326
{
 
327
  p_stderr("gist: ");
 
328
  p_stderr(msg);
 
329
  p_stderr("\ngist: try    gist -help    for usage\n");
 
330
  p_quit();
 
331
  return 1;
 
332
}
 
333
 
 
334
void Warning(char *general, char *particular)
 
335
{
 
336
  if (++warningCount > 5) return;
 
337
  if (no_warnings) {
 
338
    if (no_warnings==1)
 
339
      p_stderr("gist: (WARNING) rerun without -nowarn to see warnings");
 
340
    no_warnings= 2;
 
341
    return;
 
342
  }
 
343
  p_stderr("gist: (WARNING) ");
 
344
  p_stderr(general);
 
345
  p_stderr(particular);
 
346
  p_stderr("\n");
 
347
}
 
348
 
 
349
/* ------------------------------------------------------------------------ */
 
350
 
 
351
static int GetCommand(char *token)
 
352
{
 
353
  char **command= commandList;
 
354
  char *cmd;
 
355
  char *now= token;
 
356
  int quitOK= 0;
 
357
 
 
358
  if (!now) return -1;
 
359
 
 
360
  /* Quit has two synonyms, ? is a synonym for help, and f, b, and g
 
361
     are special command forms.  */
 
362
  if (strcmp(token, "exit")==0 || strcmp(token, "end")==0 ||
 
363
      strcmp(token, "quit")==0) {
 
364
    /* Quit and its synonyms cannot be abbreviated */
 
365
    now= "quit";
 
366
    quitOK= 1;
 
367
  } else if (strcmp(token, "?")==0) now= "help";
 
368
  else if (strcmp(token, "f")==0) now= "1f";
 
369
  else if (strcmp(token, "b")==0) now= "1b";
 
370
  else if (strcmp(token, "g")==0) now= "1g";
 
371
  else if (strcmp(token, "s")==0) now= "send";
 
372
 
 
373
  /* Check for nf, nb, ng */
 
374
  if (*now<='9' && *now>='0') {
 
375
    char *suffix;
 
376
    nPrefix= (int)strtol(now, &suffix, 10);
 
377
    cSuffix= *suffix;
 
378
    if ((cSuffix=='f' || cSuffix=='b' || cSuffix=='g') &&
 
379
        *(suffix+1)=='\0') {
 
380
      while (*command) command++;
 
381
      return command-commandList;
 
382
    }
 
383
  }
 
384
 
 
385
  cmd= *command;
 
386
  while (cmd && *now>*cmd) cmd= *(++command);
 
387
  while (cmd && *now==*cmd) {
 
388
    while (*cmd && *now==*cmd) { now++;  cmd++; }
 
389
    if (!*now) break;  /* token matches cmd */
 
390
    now= token;
 
391
    cmd= *(++command);
 
392
  }
 
393
  if (!cmd || *now) {
 
394
    p_stderr("gist: Unknown command: ");
 
395
    p_stderr(token);
 
396
    p_stderr("\n      Type help for help.\n");
 
397
    return -1;
 
398
  }
 
399
 
 
400
  if (*cmd) {
 
401
    now= token;
 
402
    cmd= *(command+1);
 
403
    if (cmd && *now++==*cmd++) {
 
404
      while (*cmd && *now==*cmd) { now++;  cmd++; }
 
405
      if (!*now) {
 
406
        p_stderr("gist: Ambiguous command: ");
 
407
        p_stderr(token);
 
408
        p_stderr("\n      Type help for help.\n");
 
409
        return -1;
 
410
      }
 
411
    }
 
412
  }
 
413
 
 
414
  if (strcmp(*command, "quit")==0 && !quitOK) {
 
415
    p_stderr("gist: The quit command cannot be abbreviated.\n");
 
416
    return -1;
 
417
  }
 
418
 
 
419
  return command-commandList;
 
420
}
 
421
 
 
422
static int GetPageGroup(char *token)
 
423
{
 
424
  int n, m, s;
 
425
  char *suffix, *tok;
 
426
 
 
427
  s= 1;
 
428
  n= m= (int)strtol(token, &suffix, 10);
 
429
  if (suffix!=token && *suffix=='-') {
 
430
    tok= suffix+1;
 
431
    n= (int)strtol(tok, &suffix, 10);
 
432
    if (suffix!=tok && *suffix=='-') {
 
433
      tok= suffix+1;
 
434
      s= (int)strtol(tok, &suffix, 10);
 
435
      if (suffix==tok) suffix= tok-1;  /* this is an error */
 
436
    }
 
437
  }
 
438
 
 
439
  if (*suffix) {
 
440
    p_stderr("gist: (SYNTAX) ");
 
441
    p_stderr(token);
 
442
    p_stderr(" is not a legal page number group\n");
 
443
    return 1;
 
444
  }
 
445
 
 
446
  if (nPageGroups>=32) {
 
447
    p_stderr("gist: (SYNTAX) too many page number groups (32 max)\n");
 
448
    return 1;
 
449
  }
 
450
 
 
451
  mPage[nPageGroups]= m;
 
452
  nPage[nPageGroups]= n;
 
453
  sPage[nPageGroups]= s;
 
454
  nPageGroups++;
 
455
  return 0;
 
456
}
 
457
 
 
458
static int CheckEOL(char *command)
 
459
{
 
460
  if (strtok(0, " \t\n")) {
 
461
    p_stderr("gist: (SYNTAX) garbage after ");
 
462
    p_stderr(command);
 
463
    p_stderr(" command\n");
 
464
    return 1;
 
465
  } else {
 
466
    return 0;
 
467
  }
 
468
}
 
469
 
 
470
static int Help(int help)
 
471
{
 
472
  int cmd;
 
473
 
 
474
  if (help) {
 
475
    p_stderr("gist: help command syntax:\n     help [command]\n");
 
476
    p_stderr("  Print help message (for given command).\n");
 
477
    return 0;
 
478
  }
 
479
 
 
480
  cmd= GetCommand(strtok(0, " \t\n"));
 
481
 
 
482
  if (cmd<0) {
 
483
    int len;
 
484
    char line[80], **command= commandList;
 
485
    p_stderr("gist: Input Syntax:\n     command [arg1] [arg2] ...\n");
 
486
    strcpy(line, "  Available commands are:  ");
 
487
    len= 27;
 
488
    for (;;) {
 
489
      for (;;) {
 
490
        if (len+strlen(*command) > 72) {
 
491
          strcpy(line+len, "\n");
 
492
          p_stderr(line);
 
493
          break;
 
494
        }
 
495
        strcpy(line+len, *command);
 
496
        len+= strlen(*command++);
 
497
        if (!*command) {
 
498
          strcpy(line+len, "\n");
 
499
          p_stderr(line);
 
500
          break;
 
501
        }
 
502
        strcpy(line+len, ", ");
 
503
        len+= 2;
 
504
      }
 
505
      if (!*command) break;
 
506
      strcpy(line, "     ");
 
507
      len= 5;
 
508
    }
 
509
    p_stderr("  The quit command has two synonyms:  exit, end\n");
 
510
    p_stderr("  Any command except quit may be abbreviated by\n");
 
511
    p_stderr("  omitting characters from the right.\n");
 
512
    p_stderr("  The help command explains specific syntax, e.g.:\n");
 
513
    p_stderr("     help cgm\n");
 
514
    p_stderr("  describes the syntax of the cgm command.\n");
 
515
    p_stderr("  Five commands can be typed to a gist X window:\n");
 
516
    p_stderr("     nf   - forward n pages and draw (default 1)\n");
 
517
    p_stderr("     nb   - backward n pages and draw (default 1)\n");
 
518
    p_stderr("     ng   - go to page n and draw (default 1)\n");
 
519
    p_stderr("     s    - send current page\n");
 
520
    p_stderr("     q    - quit\n");
 
521
 
 
522
  } else {
 
523
    CommandList[cmd](1);
 
524
  }
 
525
 
 
526
  return 0;
 
527
}
 
528
 
 
529
static char line[256];
 
530
 
 
531
void
 
532
on_stdin(char *lin)
 
533
{
 
534
  int cmd;
 
535
 
 
536
  line[0] = '\0';
 
537
  strncat(line, lin, 255);
 
538
  cmd= GetCommand(strtok(line, " \t\n"));
 
539
 
 
540
  warningCount= xPrefix= 0;
 
541
  if (cmd>=0 && CommandList[cmd](0))
 
542
    p_quit();
 
543
 
 
544
  need_prompt = 1;
 
545
}
 
546
 
 
547
static int Quit(int help)
 
548
{
 
549
  if (help) {
 
550
    p_stderr("gist: quit command syntax:\n     quit\n");
 
551
    p_stderr("  Finish and close any output files, then exit.\n");
 
552
    p_stderr("  Synonyms:  exit, end   (no abbreviations allowed)\n");
 
553
    return 0;
 
554
  }
 
555
  return 1;
 
556
}
 
557
 
 
558
/* ------------------------------------------------------------------------ */
 
559
 
 
560
static int SaveName(int device, char *name)
 
561
{
 
562
  int len;
 
563
 
 
564
  if (name == outNames[device]) return 0;
 
565
 
 
566
  len= name? (int)strlen(name) : 0;
 
567
  if (len>outLengths[device]) {
 
568
    if (outLengths[device]>0) p_free(outNames[device]);
 
569
    outNames[device]= (char *)p_malloc(len+1);
 
570
    if (!outNames[device]) {
 
571
      p_stderr("gist: (SEVERE) memory manager failed in SaveName\n");
 
572
      outLengths[device]= 0;
 
573
      return 1;
 
574
    }
 
575
    outLengths[device]= len;
 
576
  }
 
577
 
 
578
  if (name) strcpy(outNames[device], name);
 
579
  else if (outLengths[device]>0) outNames[device][0]= '\0';
 
580
  else outNames[device]= 0;
 
581
  return 0;
 
582
}
 
583
 
 
584
static int CreateCGM(int device, char *name)
 
585
{
 
586
  if (SaveName(device, name)) return 1;
 
587
 
 
588
  outEngines[device]=
 
589
    GpCGMEngine("CGM Viewer", cgmLandscape, 0, name);
 
590
  if (!outEngines[device]) {
 
591
    Warning(gistError, "");
 
592
    Warning("Unable to create CGM engine ", name);
 
593
    return 1;
 
594
  }
 
595
 
 
596
  outTypes[device]= 0;
 
597
  outDraw[device]= 0;
 
598
  outSend[device]= 1;
 
599
 
 
600
  return 0;
 
601
}
 
602
 
 
603
static int CreatePS(int device, char *name)
 
604
{
 
605
  if (SaveName(device, name)) return 1;
 
606
 
 
607
  outEngines[device]=
 
608
    GpPSEngine("CGM Viewer", cgmLandscape, 0, name);
 
609
  if (!outEngines[device]) {
 
610
    Warning(gistError, "");
 
611
    Warning("Unable to create PostScript engine ", name);
 
612
    return 1;
 
613
  }
 
614
 
 
615
  outTypes[device]= 1;
 
616
  outDraw[device]= 0;
 
617
  outSend[device]= 1;
 
618
 
 
619
  return 0;
 
620
}
 
621
 
 
622
#ifndef NO_XLIB
 
623
static int CreateX(int device, char *name)
 
624
{
 
625
  if (SaveName(device, name)) return 1;
 
626
 
 
627
  gist_input_hint = 1;
 
628
  outEngines[device]=
 
629
    GpBXEngine("CGM Viewer", cgmLandscape, defaultDPI, name);
 
630
  if (!outEngines[device]) {
 
631
    Warning(gistError, "");
 
632
    Warning("Unable to open X server ", name? name : "(default)");
 
633
    return 1;
 
634
  }
 
635
 
 
636
  outTypes[device]= 2;
 
637
  outDraw[device]= 1;
 
638
  outSend[device]= 0;
 
639
 
 
640
  GxInput(outEngines[device], &HandleExpose, 0, 0, &HandleOther);
 
641
 
 
642
  return 0;
 
643
}
 
644
#endif
 
645
 
 
646
/* ------------------------------------------------------------------------ */
 
647
 
 
648
static int OpenIn(int help)
 
649
{
 
650
  char *token;
 
651
 
 
652
  if (help) {
 
653
    p_stderr("gist: open command syntax:\n     open cgminput\n");
 
654
    p_stderr("  Closes the current CGM input file, then opens cgminput.\n");
 
655
    p_stderr("  Only a Gist-compliant binary CGM file is legal.\n");
 
656
    p_stderr("  The cgminput may be the first file of a family.\n");
 
657
    p_stderr("  Subsequent page numbers refer to this input file.\n");
 
658
    return 0;
 
659
  }
 
660
 
 
661
  token= strtok(0, " \t\n");
 
662
  if (!token) {
 
663
    p_stderr("gist: (SYNTAX) cgminput name missing in open command\n");
 
664
    return 0;
 
665
  }
 
666
  if (CheckEOL("open")) return 0;
 
667
 
 
668
  if (no_warnings) no_warnings= 1;  /* one warning per file family */
 
669
  OpenCGM(token);
 
670
  return 0;
 
671
}
 
672
 
 
673
static int FindDevice(void)
 
674
{
 
675
  int i;
 
676
  for (i=0 ; i<8 ; i++) if (!outEngines[i]) break;
 
677
  if (i>=8)
 
678
    Warning("8 devices already open for output, command ignored", "");
 
679
  return i;
 
680
}
 
681
 
 
682
static int MakeCGM(int help)
 
683
{
 
684
  char *token, *cgmout;
 
685
  long size= 0;
 
686
  int device;
 
687
 
 
688
  if (help) {
 
689
    p_stderr("gist: cgm command syntax:\n     cgm cgmout [size]\n");
 
690
    p_stderr("  Opens a CGM file cgmout for output.\n");
 
691
    p_stderr("  The size (default 1000000) is the maximum size of a\n");
 
692
    p_stderr("  single file in the output family, in bytes.\n");
 
693
    p_stderr("  Subsequent send commands will write to cgmout,\n");
 
694
    p_stderr("  unless the send to list is modified (see send).\n");
 
695
    return 0;
 
696
  }
 
697
 
 
698
  token= strtok(0, " \t\n");
 
699
  if (!token) {
 
700
    p_stderr("gist: (SYNTAX) cgmout name missing in cgm command\n");
 
701
    return 0;
 
702
  }
 
703
  cgmout= token;
 
704
  token= strtok(0, " \t\n");
 
705
  if (token) {
 
706
    char *suffix;
 
707
    size= strtol(token, &suffix, 0);
 
708
    if (*suffix) {
 
709
      p_stderr("gist: (SYNTAX) size unintelligble in cgm command\n");
 
710
      return 0;
 
711
    }
 
712
    if (CheckEOL("cgm")) return 0;
 
713
  }
 
714
 
 
715
  device= FindDevice();
 
716
  if (device>=8) return 0;
 
717
 
 
718
  if (!CreateCGM(device, cgmout) &&
 
719
      size>0) ((CGMEngine *)outEngines[device])->fileSize= size;
 
720
 
 
721
  return 0;
 
722
}
 
723
 
 
724
static int MakePS(int help)
 
725
{
 
726
  char *token;
 
727
  int device;
 
728
 
 
729
  if (help) {
 
730
    p_stderr("gist: ps command syntax:\n     ps psout\n");
 
731
    p_stderr("  Opens a PostScript file psout for output.\n");
 
732
    p_stderr("  Subsequent send commands will write to psout,\n");
 
733
    p_stderr("  unless the send to list is modified (see send).\n");
 
734
    return 0;
 
735
  }
 
736
 
 
737
  token= strtok(0, " \t\n");
 
738
  if (!token) {
 
739
    p_stderr("gist: (SYNTAX) psout name missing in ps command\n");
 
740
    return 0;
 
741
  }
 
742
  if (CheckEOL("ps")) return 0;
 
743
 
 
744
  device= FindDevice();
 
745
  if (device>=8) return 0;
 
746
 
 
747
  CreatePS(device, token);
 
748
 
 
749
  return 0;
 
750
}
 
751
 
 
752
static int MakeX(int help)
 
753
{
 
754
  char *token, *server;
 
755
  int dpi, device, defDPI;
 
756
 
 
757
  if (help) {
 
758
    p_stderr("gist: display command syntax:\n     ");
 
759
    p_stderr("display host:server.screen [dpi]\n");
 
760
    p_stderr("  Connects to the specified X server.\n");
 
761
    p_stderr("  Subsequent draw commands will write to server,\n");
 
762
    p_stderr("  unless the draw to list is modified (see draw).\n");
 
763
    p_stderr("  If specified, 40<=dpi<=200 (default 100).\n");
 
764
    return 0;
 
765
  }
 
766
 
 
767
  token= strtok(0, " \t\n");
 
768
  if (!token) {
 
769
    p_stderr("gist: (SYNTAX) cgmoutput name missing in cgm command\n");
 
770
    return 0;
 
771
  }
 
772
  server= token;
 
773
  token= strtok(0, " \t\n");
 
774
  if (token) {
 
775
    char *suffix;
 
776
    dpi= (int)strtol(token, &suffix, 0);
 
777
    if (*suffix) {
 
778
      p_stderr("gist: (SYNTAX) dpi unintelligble in display command\n");
 
779
      return 0;
 
780
    }
 
781
    if (dpi<40 && dpi>200) {
 
782
      p_stderr(
 
783
        "gist: (SYNTAX) dpi not between 40 and 200 in display command\n");
 
784
      return 0;
 
785
    }
 
786
    if (CheckEOL("display")) return 0;
 
787
  } else {
 
788
    dpi= 100;
 
789
  }
 
790
 
 
791
  device= FindDevice();
 
792
  if (device>=8) return 0;
 
793
 
 
794
  defDPI= defaultDPI;
 
795
  defaultDPI= dpi;
 
796
  CreateX(device, server);
 
797
  defaultDPI= defDPI;
 
798
 
 
799
  return 0;
 
800
}
 
801
 
 
802
static int EPS(int help)
 
803
{
 
804
  char *token;
 
805
  int device;
 
806
 
 
807
  if (help) {
 
808
    p_stderr("gist: eps command syntax:\n     eps epsout\n");
 
809
    p_stderr("  Open an Encapsulated PostScript file epsout, write\n");
 
810
    p_stderr("  the current page to it, then close epsout.\n");
 
811
    p_stderr("  (Note that an EPS file can have only a single page.)\n");
 
812
    return 0;
 
813
  }
 
814
 
 
815
  token= strtok(0, " \t\n");
 
816
  if (!token) {
 
817
    p_stderr("gist: (SYNTAX) epsout name missing in eps command\n");
 
818
    return 0;
 
819
  }
 
820
  if (CheckEOL("eps")) return 0;
 
821
 
 
822
  device= FindDevice();
 
823
  if (device>=8) return 0;
 
824
 
 
825
  device= FindDevice();
 
826
  if (device>=8) return 0;
 
827
 
 
828
  outEngines[device]=
 
829
    GpPSEngine("CGM Viewer", cgmLandscape, 0, "_tmp.eps");
 
830
  if (!outEngines[device]) {
 
831
    Warning(gistError, "");
 
832
    Warning("Unable to create PostScript engine ", token);
 
833
    return 0;
 
834
  }
 
835
 
 
836
  GpPreempt(outEngines[device]);
 
837
 
 
838
  nPage[0]= mPage[0]= CGMRelative(0);
 
839
  sPage[0]= 1;
 
840
  nPageGroups= 1;
 
841
 
 
842
  /* First read does PS part, second computes EPS preview */
 
843
  if (!ReadCGM(mPage, nPage, sPage, nPageGroups)) {
 
844
    GpPreempt(0);
 
845
    outEngines[device]= EPSPreview(outEngines[device], token);
 
846
    if (outEngines[device]) {
 
847
      GpPreempt(outEngines[device]);
 
848
      ReadCGM(mPage, nPage, sPage, nPageGroups);
 
849
    } else {
 
850
      Warning("memory manager failed creating EPS engine ", token);
 
851
      return 0;
 
852
    }
 
853
  }
 
854
 
 
855
  if (outEngines[device]) {
 
856
    GpPreempt(0);
 
857
 
 
858
    GpKillEngine(outEngines[device]);
 
859
    outEngines[device]= 0;
 
860
  }
 
861
 
 
862
  return 0;
 
863
}
 
864
 
 
865
static int FreeAny(int help)
 
866
{
 
867
  int i;
 
868
 
 
869
  if (help) {
 
870
    p_stderr("gist: free command syntax:\n     free [device# ...]\n");
 
871
    p_stderr("  Finish and close the device#(s).  If none given,\n");
 
872
    p_stderr("  frees all send devices,\n");
 
873
    p_stderr("  (Use the info command to describe current device numbers.)\n");
 
874
    return 0;
 
875
  }
 
876
 
 
877
  if (GetDeviceList(1, "free")) return 0;
 
878
 
 
879
  for (i=0 ; i<8 ; i++) {
 
880
    if (outMark[i]) {
 
881
      GpKillEngine(outEngines[i]);
 
882
      outEngines[i]= 0;
 
883
    }
 
884
  }
 
885
 
 
886
  return 0;
 
887
}
 
888
 
 
889
static char *yorn[2]= { "No  ", "Yes " };
 
890
static char *tname[3]= { "CGM", "PS ", "X  " };
 
891
 
 
892
extern void CGMinfo(void);  /* cgmin.c */
 
893
 
 
894
static int Info(int help)
 
895
{
 
896
  int i;
 
897
 
 
898
  if (help) {
 
899
    p_stderr("gist: info command syntax:\n     info\n");
 
900
    p_stderr("  Print descriptions of all current output files.\n");
 
901
    return 0;
 
902
  }
 
903
 
 
904
  p_stdout("\n");
 
905
  CGMinfo();
 
906
  p_stdout("Number Draw Send Type   Name\n");
 
907
  for (i=0 ; i<8 ; i++) {
 
908
    if (outEngines[i]) {
 
909
      char msg[80];
 
910
      sprintf(msg, "%3d    %s %s %s  ", i, yorn[outDraw[i]], yorn[outSend[i]],
 
911
             tname[outTypes[i]]);
 
912
      p_stdout(msg);
 
913
      p_stdout(outNames[i]? outNames[i] : "");
 
914
      p_stdout("\n");
 
915
    }
 
916
  }
 
917
  p_stdout("\n");
 
918
 
 
919
  return 0;
 
920
}
 
921
 
 
922
/* ------------------------------------------------------------------------ */
 
923
 
 
924
static int Draw(int help)
 
925
{
 
926
  int i, n;
 
927
  char *token;
 
928
 
 
929
  if (help) {
 
930
    p_stderr("gist: draw command syntax:\n     draw [page list]\n");
 
931
    p_stderr(
 
932
    "  Copy the page(s) (default current page) from the current CGM input\n");
 
933
    p_stderr("  to all display output devices.\n");
 
934
    p_stderr("  By default, these are all X windows.\n");
 
935
    p_stderr("  Use alternate syntax:\n     draw to [device#1 ...]\n");
 
936
    p_stderr("  to specify a particular list of devices to be used\n");
 
937
    p_stderr("  by the draw command.  Without any device numbers,\n");
 
938
    p_stderr("  draw to restores the default list of devices.\n");
 
939
    p_stderr("  (Use the info command to describe current device numbers.)\n");
 
940
    p_stderr("  Page list syntax:   group1 [group2 ...]\n");
 
941
    p_stderr("  Page group syntax:   n   - just page n\n");
 
942
    p_stderr("                     m-n   - pages n thru m\n");
 
943
    p_stderr("                   m-n-s   - pages n thru m, step s\n");
 
944
    return 0;
 
945
  }
 
946
 
 
947
  token= strtok(0, " \t\n");
 
948
  if (token && strcmp(token, "to")==0) {
 
949
    DrawSend(1, token);
 
950
    return 0;
 
951
  }
 
952
 
 
953
  n= 0;
 
954
  for (i=0 ; i<8 ; i++) {
 
955
    if (!outDraw[i]) GpDeactivate(outEngines[i]);
 
956
    if (outDraw[i] && !GpActivate(outEngines[i])) n++;
 
957
  }
 
958
 
 
959
  if (!n && (i= FindDevice())<8) {
 
960
    if (!CreateX(i, 0) && !GpActivate(outEngines[i])) n++;
 
961
  }
 
962
 
 
963
  if (n) DrawSend(0, token);
 
964
  else Warning("no devices active for draw command", "");
 
965
  return 0;
 
966
}
 
967
 
 
968
static int Send(int help)
 
969
{
 
970
  int i, n;
 
971
  char *token;
 
972
 
 
973
  if (help) {
 
974
    p_stderr("gist: send command syntax:\n     send [page list]\n");
 
975
    p_stderr(
 
976
    "  Copy the page(s) (default current page) from the current CGM input\n");
 
977
    p_stderr("  to all display output devices.\n");
 
978
    p_stderr("  By default, these are all X windows.\n");
 
979
    p_stderr("  Use alternate syntax:\n     send to [device#1] ...\n");
 
980
    p_stderr("  to specify a particular list of devices to be used\n");
 
981
    p_stderr("  by the send command.  Without any device numbers,\n");
 
982
    p_stderr("  send to restores the default list of devices.\n");
 
983
    p_stderr("  (Use the info command to describe current device numbers.)\n");
 
984
    p_stderr("  Page list syntax:   group1 [group2 ...]\n");
 
985
    p_stderr("  Page group syntax:   n   - just page n\n");
 
986
    p_stderr("                     m-n   - pages n thru m\n");
 
987
    p_stderr("                   m-n-s   - pages n thru m, step s\n");
 
988
    return 0;
 
989
  }
 
990
 
 
991
  token= strtok(0, " \t\n");
 
992
  if (token && strcmp(token, "to")==0) {
 
993
    DrawSend(1, token);
 
994
    return 0;
 
995
  }
 
996
 
 
997
  n= 0;
 
998
  for (i=0 ; i<8 ; i++) {
 
999
    if (!outSend[i]) GpDeactivate(outEngines[i]);
 
1000
    if (outSend[i] && !GpActivate(outEngines[i])) n++;
 
1001
  }
 
1002
 
 
1003
  if (n) DrawSend(1, token);
 
1004
  else Warning("no devices active for send command", "");
 
1005
  return 0;
 
1006
}
 
1007
 
 
1008
static int GetDeviceList(int ds, char *command)
 
1009
{
 
1010
  int device;
 
1011
  char *token= strtok(0, " \t\n");
 
1012
 
 
1013
  if (token) {
 
1014
    char *suffix;
 
1015
    for (device=0 ; device<8 ; device++) outMark[device]= 0;
 
1016
    do {
 
1017
      device= (int)strtol(token, &suffix, 10);
 
1018
      if (*suffix || device<0 || device>7) {
 
1019
        p_stderr("gist: (SYNTAX) ");
 
1020
        p_stderr(token);
 
1021
        p_stderr(" not a legal device# in ");
 
1022
        p_stderr(command);
 
1023
        p_stderr(" command\n");
 
1024
        return 1;
 
1025
      }
 
1026
      if (!outEngines[device])
 
1027
        Warning("ignoring non-existent device# ", token);
 
1028
      else
 
1029
        outMark[device]= 1;
 
1030
    } while ((token= strtok(0, " \t\n")));
 
1031
    if (ds==0) {
 
1032
      for (device=0 ; device<8 ; device++) if (outMark[device]) break;
 
1033
      if (device>=8) {
 
1034
        Warning(command, " command with no legal devices, no action taken");
 
1035
        return 1;
 
1036
      }
 
1037
    }
 
1038
 
 
1039
  } else if (ds==0) {
 
1040
    for (device=0 ; device<8 ; device++) outMark[device]= 0;
 
1041
  } else {
 
1042
    for (device=0 ; device<8 ; device++) outMark[device]= outSend[device];
 
1043
  }
 
1044
 
 
1045
  return 0;
 
1046
}
 
1047
 
 
1048
static char *dsName[]= { "draw to", "send to" };
 
1049
 
 
1050
static void DrawSend(int ds, char *token)
 
1051
{
 
1052
  nPageGroups= 0;
 
1053
 
 
1054
  if (token && strcmp(token, "to")==0) {
 
1055
    /* draw to  or send to  merely resets outDraw or outSend list */
 
1056
    int i, n= 0;
 
1057
    int *outDS= ds? outSend : outDraw;
 
1058
    if (GetDeviceList(0, dsName[ds])) return;
 
1059
    for (i=0 ; i<8 ; i++) if ((outDS[i]= outMark[i])) n++;
 
1060
    if (!n) for (i=0 ; i<8 ; i++)
 
1061
      outDS[i]= outEngines[i]? (ds? outTypes[i]<2 : outTypes[i]==2) : 0;
 
1062
    return;
 
1063
 
 
1064
  } else if (token) {
 
1065
    do {
 
1066
      if (GetPageGroup(token)) return;
 
1067
    } while ((token= strtok(0, " \t\n")));
 
1068
  } else {
 
1069
    nPage[0]= mPage[0]= CGMRelative(0);
 
1070
    sPage[0]= 1;
 
1071
    nPageGroups= 1;
 
1072
  }
 
1073
 
 
1074
  ReadCGM(mPage, nPage, sPage, nPageGroups);
 
1075
}
 
1076
 
 
1077
static int Special(int help)
 
1078
{
 
1079
  if (help) {
 
1080
    char msg[80];
 
1081
    sprintf(msg, "gist: n%c command syntax:\n     n%c\n",
 
1082
            cSuffix, cSuffix);
 
1083
    p_stderr(msg);
 
1084
    if (cSuffix=='f')
 
1085
      p_stderr("  Forward n (default 1) pages, then draw\n");
 
1086
    else if (cSuffix=='b')
 
1087
      p_stderr("  Backward n (default 1) pages, then draw\n");
 
1088
    else
 
1089
      p_stderr("  Go to page n (default 1), then draw\n");
 
1090
    return 0;
 
1091
  }
 
1092
 
 
1093
  if (CheckEOL("nf, nb, or ng")) return 0;
 
1094
 
 
1095
  DoSpecial(nPrefix, cSuffix);
 
1096
  return 0;
 
1097
}
 
1098
 
 
1099
static void DoSpecial(int nPrefix, int cSuffix)
 
1100
{
 
1101
  int i, n;
 
1102
 
 
1103
  n= 0;
 
1104
  for (i=0 ; i<8 ; i++) {
 
1105
    if (!outDraw[i]) GpDeactivate(outEngines[i]);
 
1106
    if (outDraw[i] && !GpActivate(outEngines[i])) n++;
 
1107
  }
 
1108
 
 
1109
  if (cSuffix=='f') mPage[0]= CGMRelative(nPrefix);
 
1110
  else if (cSuffix=='b') mPage[0]= CGMRelative(-nPrefix);
 
1111
  else mPage[0]= nPrefix;
 
1112
  nPage[0]= mPage[0];
 
1113
  sPage[0]= 1;
 
1114
  nPageGroups= 1;
 
1115
 
 
1116
  if (n) ReadCGM(mPage, nPage, sPage, nPageGroups);
 
1117
  else Warning("no devices active for nf, nb, or ng command", "");
 
1118
}
 
1119
 
 
1120
/* ------------------------------------------------------------------------ */
 
1121
 
 
1122
/* ARGSUSED */
 
1123
static void HandleExpose(Engine *engine, Drauing *drauing, int xy[])
 
1124
{
 
1125
  XEngine *xEngine= GisXEngine(engine);
 
1126
 
 
1127
  if (!xEngine) return;
 
1128
 
 
1129
  /* Redraw current picture on this engine only */
 
1130
 
 
1131
  GpPreempt(engine);
 
1132
 
 
1133
  nPage[0]= mPage[0]= CGMRelative(0);
 
1134
  sPage[0]= 1;
 
1135
  nPageGroups= 1;
 
1136
 
 
1137
  ReadCGM(mPage, nPage, sPage, nPageGroups);
 
1138
 
 
1139
  GpPreempt(0);
 
1140
  GxExpose(engine, drauing, xy);
 
1141
}
 
1142
 
 
1143
static int cSuffices[]= { 'f', 'b', 'g' };
 
1144
 
 
1145
/* ARGSUSED */
 
1146
static void HandleOther(Engine *engine, int k, int md)
 
1147
{
 
1148
  XEngine *xEngine= GisXEngine(engine);
 
1149
  int go, bad;
 
1150
 
 
1151
  if (!xEngine) return;
 
1152
 
 
1153
  go= bad= 0;
 
1154
 
 
1155
  if (k == '0') xPrefix= 10*xPrefix;
 
1156
  else if (k == '1') xPrefix= 10*xPrefix+1;
 
1157
  else if (k == '2') xPrefix= 10*xPrefix+2;
 
1158
  else if (k == '3') xPrefix= 10*xPrefix+3;
 
1159
  else if (k == '4') xPrefix= 10*xPrefix+4;
 
1160
  else if (k == '5') xPrefix= 10*xPrefix+5;
 
1161
  else if (k == '6') xPrefix= 10*xPrefix+6;
 
1162
  else if (k == '7') xPrefix= 10*xPrefix+7;
 
1163
  else if (k == '8') xPrefix= 10*xPrefix+8;
 
1164
  else if (k == '9') xPrefix= 10*xPrefix+9;
 
1165
  else if (k=='f' || k=='F' || (k=='+' && (md&P_KEYPAD))) go= 1;
 
1166
  else if (k=='b' || k=='B' || (k=='-' && (md&P_KEYPAD))) go= 2;
 
1167
  else if (k=='g' || k=='G' || k=='\r') go= 3;
 
1168
  else if (k=='s' || k=='S' || (k=='=' && (md&P_KEYPAD))) go= 4;
 
1169
  else if (k=='q' || k=='Q') go= 5;
 
1170
  else bad= 1;
 
1171
 
 
1172
  if ((go==4||go==5) && xPrefix!=0) bad= 1;
 
1173
  if (go && !bad) {
 
1174
    if (go<4) {
 
1175
      if (xPrefix==0) xPrefix= 1;
 
1176
      DoSpecial(xPrefix, cSuffices[go-1]);
 
1177
    } else if (go==4) {
 
1178
      int i, n= 0;
 
1179
      for (i=0 ; i<8 ; i++) {
 
1180
        if (!outSend[i]) GpDeactivate(outEngines[i]);
 
1181
        if (outSend[i] && !GpActivate(outEngines[i])) n++;
 
1182
      }
 
1183
 
 
1184
      nPage[0]= mPage[0]= CGMRelative(0);
 
1185
      sPage[0]= 1;
 
1186
      nPageGroups= 1;
 
1187
 
 
1188
      if (n) ReadCGM(mPage, nPage, sPage, nPageGroups);
 
1189
      else Warning("no devices active for send command", "");
 
1190
    } else if (go==5) {
 
1191
      p_quit();
 
1192
    }
 
1193
    xPrefix= 0;
 
1194
    warningCount= 0;
 
1195
  } else if (bad) {
 
1196
    p_feep(xEngine->win);
 
1197
    xPrefix= 0;
 
1198
  }
 
1199
}
 
1200
 
 
1201
/* ------------------------------------------------------------------------ */