| Report problems to ATLAS LXR Team (with time and IP address indicated) |
|
[ source navigation ] [ diff markup ] [ identifier search ] [ general search ] |
||||
|
||||||
| Links to LXR source navigation pages for stable releases | [ 12.*.* ] [ 13.*.* ] [ 14.*.* ] | |||||
001 /* 002 * BROWSER.C 003 * 004 * $Id: browser.c,v 1.1 2008/04/08 17:40:13 binet Exp $ 005 * 006 * Main for GIST CGM viewer 007 * 008 */ 009 /* Copyright (c) 1994. The Regents of the University of California. 010 All rights reserved. */ 011 012 #include <string.h> 013 #include "pstdlib.h" 014 #include "pstdio.h" 015 016 #include "xbasic.h" 017 #include "cgm.h" 018 019 /* OpenCGM, ReadCGM, CGMRelative, cgmLandscape, bg0fg1 in cgmin.h */ 020 #include "cgmin.h" 021 /* GpEPSEngine declared in eps.h */ 022 #include "eps.h" 023 024 extern long strtol(const char *, char **, int); 025 026 extern int cgmScaleToFit; 027 extern int gtDoEscapes; 028 029 /* List of commands in alphabetical order, 0 terminated */ 030 static char *commandList[]= { 031 "cgm", "display", "draw", "eps", "free", "help", "info", "open", 032 "ps", "quit", "send", 0 }; 033 034 /* Corresponding functions */ 035 static int MakeCGM(int help); 036 static int MakeX(int help); 037 static int Draw(int help); 038 static int EPS(int help); 039 static int FreeAny(int help); 040 static int Help(int help); 041 static int Info(int help); 042 static int OpenIn(int help); 043 static int MakePS(int help); 044 static int Quit(int help); 045 static int Send(int help); 046 static int Special(int help); /* must correspond to 0 in commandList */ 047 static int nPrefix, cSuffix; /* arguments for Special */ 048 typedef int (*Command)(int help); 049 static Command CommandList[]= { 050 &MakeCGM, &MakeX, &Draw, &EPS, &FreeAny, &Help, &Info, &OpenIn, 051 &MakePS, &Quit, &Send, &Special }; 052 static int GetCommand(char *token); 053 054 int amBatch= 0; 055 056 int warningCount= 0; 057 int no_warnings= 0; 058 059 static int CreateCGM(int device, char *name); 060 static int CreatePS(int device, char *name); 061 static int CreateX(int device, char *name); 062 063 static int GetPageGroup(char *token); 064 static int CheckEOL(char *command); 065 static void DrawSend(int ds, char *token); 066 static int GetDeviceList(int ds, char *command); 067 static int FindDevice(void); 068 static int SaveName(int device, char *name); 069 static void DoSpecial(int nPrefix, int cSuffix); 070 071 static int xPrefix= 0; 072 static void HandleExpose(Engine *engine, Drauing *drauing, int xy[]); 073 static void HandleOther(Engine *engine, int k, int md); 074 static int HelpAndExit(void); 075 static int MessageAndExit(char *msg); 076 077 extern int on_idle(void); 078 extern int on_quit(void); 079 extern void on_stdin(char *line); 080 static int did_startup = 0; 081 extern int do_startup(void); 082 083 int defaultDPI; 084 085 char *outNames[8]; 086 int outLengths[8]; 087 int outDraw[8], outSend[8], outMark[8]; 088 089 int nPageGroups= 0; 090 int mPage[32], nPage[32], sPage[32]; 091 int n_active_groups = 0; 092 093 static int nOut, noDisplay, x_only; 094 static char *inName; 095 096 int 097 on_launch(int argc, char *argv[]) 098 { 099 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