examples: Add ".pcb" extension to "PCB(2)", move "LED.NET" to "LED.net"
[geda-pcb/gde.git] / src / main.c
bloba0b38f3f657256ae81af71c7974a86843d8215c1
1 /* $Id$ */
3 /*
4 * COPYRIGHT
6 * PCB, interactive printed circuit board design
7 * Copyright (C) 1994,1995,1996, 2004 Thomas Nau
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Contact addresses for paper mail and Email:
24 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
25 * Thomas.Nau@rz.uni-ulm.de
30 /* main program, initializes some stuff and handles user input
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <stdlib.h>
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #include <signal.h>
41 #include <unistd.h>
42 #include <sys/stat.h>
44 #include "global.h"
45 #include "data.h"
46 #include "buffer.h"
47 #include "create.h"
48 #include "crosshair.h"
49 #include "draw.h"
50 #include "error.h"
51 #include "file.h"
52 #include "set.h"
53 #include "action.h"
54 #include "misc.h"
55 #include "lrealpath.h"
57 /* This next one is so we can print the help messages. */
58 #include "hid/hidint.h"
60 #ifdef HAVE_DBUS
61 #include "dbus.h"
62 #endif
64 #if ENABLE_NLS
65 #include <libintl.h>
66 #endif
68 #ifdef HAVE_LIBDMALLOC
69 #include <dmalloc.h>
70 #endif
72 RCSID ("$Id$");
75 #define PCBLIBPATH ".:" PCBLIBDIR
78 #ifdef HAVE_LIBSTROKE
79 extern void stroke_init (void);
80 #endif
83 /* ----------------------------------------------------------------------
84 * initialize signal and error handlers
86 static void
87 InitHandler (void)
90 signal(SIGHUP, CatchSignal);
91 signal(SIGQUIT, CatchSignal);
92 signal(SIGABRT, CatchSignal);
93 signal(SIGSEGV, CatchSignal);
94 signal(SIGTERM, CatchSignal);
95 signal(SIGINT, CatchSignal);
98 /* calling external program by popen() may cause a PIPE signal,
99 * so we ignore it
101 #ifdef SIGPIPE
102 signal (SIGPIPE, SIG_IGN);
103 #endif
107 /* ----------------------------------------------------------------------
108 | command line and rc file processing.
110 static char *command_line_pcb;
112 void
113 copyright (void)
115 printf ("\n"
116 " COPYRIGHT for %s version %s\n\n"
117 " PCB, interactive printed circuit board design\n"
118 " Copyright (C) 1994,1995,1996,1997 Thomas Nau\n"
119 " Copyright (C) 1998, 1999, 2000 Harry Eaton\n\n"
120 " This program is free software; you can redistribute it and/or modify\n"
121 " it under the terms of the GNU General Public License as published by\n"
122 " the Free Software Foundation; either version 2 of the License, or\n"
123 " (at your option) any later version.\n\n"
124 " This program is distributed in the hope that it will be useful,\n"
125 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
126 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
127 " GNU General Public License for more details.\n\n"
128 " You should have received a copy of the GNU General Public License\n"
129 " along with this program; if not, write to the Free Software\n"
130 " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n",
131 Progname, VERSION);
132 exit (0);
135 static inline void
136 u (const char *fmt, ...)
138 va_list ap;
139 va_start (ap, fmt);
140 vfprintf (stderr, fmt, ap);
141 fputc ('\n', stderr);
142 va_end (ap);
145 static void
146 usage_attr (HID_Attribute * a)
148 int i;
149 static char buf[200];
151 if (a->help_text == ATTR_UNDOCUMENTED)
152 return;
154 switch (a->type)
156 case HID_Label:
157 return;
158 case HID_Integer:
159 case HID_Real:
160 sprintf (buf, "--%s <num>", a->name);
161 break;
162 case HID_String:
163 sprintf (buf, "--%s <string>", a->name);
164 break;
165 case HID_Boolean:
166 sprintf (buf, "--%s", a->name);
167 break;
168 case HID_Mixed:
169 case HID_Enum:
170 sprintf (buf, "--%s ", a->name);
171 if (a->type == HID_Mixed)
172 strcat (buf, " <val>");
173 for (i = 0; a->enumerations[i]; i++)
175 strcat (buf, i ? "|" : "<");
176 strcat (buf, a->enumerations[i]);
178 strcat (buf, ">");
179 break;
180 case HID_Path:
181 sprintf (buf, "--%s <path>", a->name);
182 break;
185 if (strlen (buf) <= 30)
187 if (a->help_text)
188 fprintf (stderr, " %-30s\t%s\n", buf, a->help_text);
189 else
190 fprintf (stderr, " %-30s\n", buf);
192 else if (a->help_text && strlen (a->help_text) + strlen (buf) < 72)
193 fprintf (stderr, " %s\t%s\n", buf, a->help_text);
194 else if (a->help_text)
195 fprintf (stderr, " %s\n\t\t\t%s\n", buf, a->help_text);
196 else
197 fprintf (stderr, " %s\n", buf);
200 static void
201 usage_hid (HID * h)
203 HID_Attribute *e;
204 int i, n;
206 if (h->gui)
208 HID **hl = hid_enumerate ();
209 HID_AttrNode *ha;
210 fprintf (stderr, "\ngui options:\n");
211 for (ha = hid_attr_nodes; ha; ha = ha->next)
213 for (i = 0; hl[i]; i++)
214 if (ha->attributes == hl[i]->get_export_options (0))
215 goto skip_this_one;
216 for (i = 0; i < ha->n; i++)
217 usage_attr (ha->attributes + i);
218 skip_this_one:;
220 return;
222 fprintf (stderr, "\n%s options:\n", h->name);
223 exporter = h;
224 e = h->get_export_options (&n);
225 if (!e)
226 return;
227 for (i = 0; i < n; i++)
228 usage_attr (e + i);
229 exporter = NULL;
232 static void
233 usage (void)
235 HID **hl = hid_enumerate ();
236 int i;
237 int n_printer = 0, n_gui = 0, n_exporter = 0;
239 for (i = 0; hl[i]; i++)
241 if (hl[i]->gui)
242 n_gui++;
243 if (hl[i]->printer)
244 n_printer++;
245 if (hl[i]->exporter)
246 n_exporter++;
249 u ("PCB Printed Circuit Board editing program, http://pcb.sourceforge.net");
250 u ("%s [-h|-V|--copyright]\t\t\tHelp, version, copyright", Progname);
251 u ("%s [gui options] <pcb file>\t\tto edit", Progname);
252 u ("Available GUI hid%s:", n_gui == 1 ? "" : "s");
253 for (i = 0; hl[i]; i++)
254 if (hl[i]->gui)
255 fprintf (stderr, "\t%s\t%s\n", hl[i]->name, hl[i]->description);
256 u ("%s -p [printing options] <pcb file>\tto print", Progname);
257 u ("Available printing hid%s:", n_printer == 1 ? "" : "s");
258 for (i = 0; hl[i]; i++)
259 if (hl[i]->printer)
260 fprintf (stderr, "\t%s\t%s\n", hl[i]->name, hl[i]->description);
261 u ("%s -x hid [export options] <pcb file>\tto export", Progname);
262 u ("Available export hid%s:", n_exporter == 1 ? "" : "s");
263 for (i = 0; hl[i]; i++)
264 if (hl[i]->exporter)
265 fprintf (stderr, "\t%s\t%s\n", hl[i]->name, hl[i]->description);
267 for (i = 0; hl[i]; i++)
268 if (hl[i]->gui)
269 usage_hid (hl[i]);
270 for (i = 0; hl[i]; i++)
271 if (hl[i]->printer)
272 usage_hid (hl[i]);
273 for (i = 0; hl[i]; i++)
274 if (hl[i]->exporter)
275 usage_hid (hl[i]);
276 exit (1);
279 static void
280 print_defaults_1 (HID_Attribute * a, void *value)
282 int i;
283 double d;
284 char *s;
286 /* Remember, at this point we've parsed the command line, so they
287 may be in the global variable instead of the default_val. */
288 switch (a->type)
290 case HID_Integer:
291 i = value ? *(int *) value : a->default_val.int_value;
292 fprintf (stderr, "%s %d\n", a->name, i);
293 break;
294 case HID_Boolean:
295 i = value ? *(char *) value : a->default_val.int_value;
296 fprintf (stderr, "%s %s\n", a->name, i ? "yes" : "no");
297 break;
298 case HID_Real:
299 d = value ? *(double *) value : a->default_val.real_value;
300 fprintf (stderr, "%s %g\n", a->name, d);
301 break;
302 case HID_String:
303 case HID_Path:
304 s = value ? *(char **) value : a->default_val.str_value;
305 fprintf (stderr, "%s \"%s\"\n", a->name, s);
306 break;
307 case HID_Enum:
308 i = value ? *(int *) value : a->default_val.int_value;
309 fprintf (stderr, "%s %s\n", a->name, a->enumerations[i]);
310 break;
311 case HID_Mixed:
312 i = value ? *(int *) value : a->default_val.int_value;
313 d = value ? *(double *) value : a->default_val.real_value;
314 fprintf (stderr, "%s %g%s\n", a->name, d, a->enumerations[i]);
315 break;
316 case HID_Label:
317 break;
321 static void
322 print_defaults ()
324 HID **hl = hid_enumerate ();
325 HID_Attribute *e;
326 int i, n, hi;
328 for (hi = 0; hl[hi]; hi++)
330 HID *h = hl[hi];
331 if (h->gui)
333 HID_AttrNode *ha;
334 fprintf (stderr, "\ngui defaults:\n");
335 for (ha = hid_attr_nodes; ha; ha = ha->next)
336 for (i = 0; i < ha->n; i++)
337 print_defaults_1 (ha->attributes + i, ha->attributes[i].value);
339 else
341 fprintf (stderr, "\n%s defaults:\n", h->name);
342 e = h->get_export_options (&n);
343 if (e)
344 for (i = 0; i < n; i++)
345 print_defaults_1 (e + i, 0);
348 exit (1);
351 /* in hid/common/actions.c */
352 extern void print_actions ();
355 #define SSET(F,D,N,H) { N, H, \
356 HID_String, 0, 0, { 0, D, 0 }, 0, &Settings.F }
357 #define ISET(F,D,N,H) { N, H, \
358 HID_Integer, 0, 0, { D, 0, 0 }, 0, &Settings.F }
359 #define BSET(F,D,N,H) { N, H, \
360 HID_Boolean, 0, 0, { D, 0, 0 }, 0, &Settings.F }
361 #define RSET(F,D,N,H) { N, H, \
362 HID_Real, 0, 0, { 0, 0, D }, 0, &Settings.F }
364 #define COLOR(F,D,N,H) { N, H, \
365 HID_String, 0, 0, { 0, D, 0 }, 0, &Settings.F }
366 #define LAYERCOLOR(N,D) { "layer-color-" #N, "Color for layer " #N, \
367 HID_String, 0, 0, { 0, D, 0 }, 0, &Settings.LayerColor[N-1] }
368 #define LAYERNAME(N,D) { "layer-name-" #N, "Name for layer " #N, \
369 HID_String, 0, 0, { 0, D, 0 }, 0, &Settings.DefaultLayerName[N-1] }
370 #define LAYERSELCOLOR(N) { "layer-selected-color-" #N, "Color for layer " #N " when selected", \
371 HID_String, 0, 0, { 0, "#00ffff", 0 }, 0, &Settings.LayerSelectedColor[N-1] }
373 static int show_help = 0;
374 static int show_version = 0;
375 static int show_copyright = 0;
376 static int show_defaults = 0;
377 static int show_actions = 0;
378 static int do_dump_actions = 0;
380 HID_Attribute main_attribute_list[] = {
381 {"help", "Show Help", HID_Boolean, 0, 0, {0, 0, 0}, 0, &show_help},
382 {"version", "Show Version", HID_Boolean, 0, 0, {0, 0, 0}, 0, &show_version},
383 {"verbose", "Be verbose", HID_Boolean, 0, 0, {0, 0, 0}, 0,
384 &Settings.verbose},
385 {"copyright", "Show Copyright", HID_Boolean, 0, 0, {0, 0, 0}, 0,
386 &show_copyright},
387 {"show-defaults", "Show option defaults", HID_Boolean, 0, 0, {0, 0, 0}, 0,
388 &show_defaults},
389 {"show-actions", "Show actions", HID_Boolean, 0, 0, {0, 0, 0}, 0,
390 &show_actions},
391 {"dump-actions", "Dump actions (for documentation)", HID_Boolean, 0, 0,
392 {0, 0, 0}, 0,
393 &do_dump_actions},
395 BSET (grid_units_mm, 0, "grid-units-mm", 0),
397 COLOR (BlackColor, "#000000", "black-color", "color for black"),
398 COLOR (WhiteColor, "#ffffff", "white-color", "color for white"),
399 COLOR (BackgroundColor, "#e5e5e5", "background-color",
400 "color for background"),
401 COLOR (CrosshairColor, "#ff0000", "crosshair-color",
402 "color for the crosshair"),
403 COLOR (CrossColor, "#cdcd00", "cross-color", "color for cross"),
404 COLOR (ViaColor, "#7f7f7f", "via-color", "color for vias"),
405 COLOR (ViaSelectedColor, "#00ffff", "via-selected-color",
406 "color for selected vias"),
407 COLOR (PinColor, "#4d4d4d", "pin-color", "color for pins"),
408 COLOR (PinSelectedColor, "#00ffff", "pin-selected-color",
409 "color for selected pins"),
410 COLOR (PinNameColor, "#ff0000", "pin-name-color",
411 "color for pin names and numbers"),
412 COLOR (ElementColor, "#000000", "element-color", "color for elements"),
413 COLOR (RatColor, "#b8860b", "rat-color", "color for ratlines"),
414 COLOR (InvisibleObjectsColor, "#cccccc", "invisible-objects-color",
415 "color for invisible objects"),
416 COLOR (InvisibleMarkColor, "#cccccc", "invisible-mark-color",
417 "color for invisible marks"),
418 COLOR (ElementSelectedColor, "#00ffff", "element-selected-color",
419 "color for selected elements"),
420 COLOR (RatSelectedColor, "#00ffff", "rat-selected-color",
421 "color for selected rats"),
422 COLOR (ConnectedColor, "#00ff00", "connected-color",
423 "color for connections"),
424 COLOR (OffLimitColor, "#cccccc", "off-limit-color",
425 "color for off-limits areas"),
426 COLOR (GridColor, "#ff0000", "grid-color", "color for the grid"),
427 LAYERCOLOR (1, "#8b2323"),
428 LAYERCOLOR (2, "#3a5fcd"),
429 LAYERCOLOR (3, "#104e8b"),
430 LAYERCOLOR (4, "#cd3700"),
431 LAYERCOLOR (5, "#548b54"),
432 LAYERCOLOR (6, "#8b7355"),
433 LAYERCOLOR (7, "#00868b"),
434 LAYERCOLOR (8, "#228b22"),
435 LAYERCOLOR (9, "#8b2323"),
436 LAYERCOLOR (10, "#3a5fcd"),
437 LAYERCOLOR (11, "#104e8b"),
438 LAYERCOLOR (12, "#cd3700"),
439 LAYERCOLOR (13, "#548b54"),
440 LAYERCOLOR (14, "#8b7355"),
441 LAYERCOLOR (15, "#00868b"),
442 LAYERCOLOR (16, "#228b22"),
443 LAYERSELCOLOR (1),
444 LAYERSELCOLOR (2),
445 LAYERSELCOLOR (3),
446 LAYERSELCOLOR (4),
447 LAYERSELCOLOR (5),
448 LAYERSELCOLOR (6),
449 LAYERSELCOLOR (7),
450 LAYERSELCOLOR (8),
451 LAYERSELCOLOR (9),
452 LAYERSELCOLOR (10),
453 LAYERSELCOLOR (11),
454 LAYERSELCOLOR (12),
455 LAYERSELCOLOR (13),
456 LAYERSELCOLOR (14),
457 LAYERSELCOLOR (15),
458 LAYERSELCOLOR (16),
459 COLOR (WarnColor, "#ff8000", "warn-color", "color for warnings"),
460 COLOR (MaskColor, "#ff0000", "mask-color", "color for solder mask"),
462 ISET (ViaThickness, 6000, "via-thickness", 0),
463 ISET (ViaDrillingHole, 2800, "via-drilling-hole", 0),
464 ISET (LineThickness, 1000, "line-thickness",
465 "Initial thickness of new lines."),
466 ISET (RatThickness, 1000, "rat-thickness", 0),
467 ISET (Keepaway, 1000, "keepaway", 0),
468 ISET (MaxWidth, 600000, "default-PCB-width", 0),
469 ISET (MaxHeight, 500000, "default-PCB-height", 0),
470 ISET (TextScale, 100, "text-scale", 0),
471 ISET (AlignmentDistance, 200, "alignment-distance", 0),
472 ISET (Bloat, 1000, "bloat", 0),
473 ISET (Shrink, 1000, "shrink", 0),
474 ISET (minWid, 1000, "min-width", "DRC minimum copper spacing"),
475 ISET (minSlk, 1000, "min-silk", "DRC minimum silk width"),
476 ISET (minDrill, 1500, "min-drill", "DRC minimum drill diameter"),
477 ISET (minRing, 1000, "min-ring", "DRC minimum annular ring"),
479 RSET (Grid, 1000, "grid", 0),
480 RSET (grid_increment_mm, 0.1, "grid-increment-mm", 0),
481 RSET (grid_increment_mil, 5.0, "grid-increment-mil", 0),
482 RSET (size_increment_mm, 0.2, "size-increment-mm", 0),
483 RSET (size_increment_mil, 10.0, "size-increment-mil", 0),
484 RSET (line_increment_mm, 0.1, "line-increment-mm", 0),
485 RSET (line_increment_mil, 5.0, "line-increment-mil", 0),
486 RSET (clear_increment_mm, 0.05, "clear-increment-mm", 0),
487 RSET (clear_increment_mil, 2.0, "clear-increment-mil", 0),
488 RSET (IsleArea, 200000000, "minimum polygon area", 0),
490 ISET (BackupInterval, 60, "backup-interval", 0),
492 LAYERNAME (1, "component"),
493 LAYERNAME (2, "solder"),
494 LAYERNAME (3, "GND"),
495 LAYERNAME (4, "power"),
496 LAYERNAME (5, "signal1"),
497 LAYERNAME (6, "signal2"),
498 LAYERNAME (7, "signal3"),
499 LAYERNAME (8, "signal4"),
501 SSET (FontCommand, "",
502 "font-command", 0),
503 SSET (FileCommand, "", "file-command", "Command to read a file."),
504 SSET (ElementCommand,
505 "M4PATH='%p';export M4PATH;echo 'include(%f)' | " GNUM4,
506 "element-command", 0),
507 SSET (PrintFile, "%f.output", "print-file", 0),
508 SSET (LibraryCommandDir, PCBLIBDIR, "lib-command-dir", 0),
509 SSET (LibraryCommand, "QueryLibrary.sh '%p' '%f' %a",
510 "lib-command", 0),
511 SSET (LibraryContentsCommand, "ListLibraryContents.sh '%p' '%f'",
512 "lib-contents-command", 0),
513 SSET (LibraryTree, PCBTREEPATH, "lib-newlib",
514 "Top level directory for the newlib style library"),
515 SSET (SaveCommand, "", "save-command", 0),
516 SSET (LibraryFilename, LIBRARYFILENAME, "lib-name", 0),
517 SSET (FontFile, "default_font", "default-font",
518 "File name of default font."),
519 SSET (Groups, "1,c:2,s:3:4:5:6:7:8", "groups", 0),
520 SSET (Routes, "Signal,1000,3600,2000,1000:Power,2500,6000,3500,1000"
521 ":Fat,4000,6000,3500,1000:Skinny,600,2402,1181,600", "route-styles",
523 SSET (FilePath, "", "file-path", 0),
524 SSET (RatCommand, "", "rat-command", 0),
525 SSET (FontPath, PCBLIBPATH, "font-path", 0),
526 SSET (ElementPath, PCBLIBPATH, "element-path", 0),
527 SSET (LibraryPath, PCBLIBPATH, "lib-path", 0),
528 SSET (MenuFile, "pcb-menu.res", "menu-file", 0),
529 SSET (ScriptFilename, 0, "action-script",
530 "If set, this file is executed at startup."),
531 SSET (ActionString, 0, "action-string",
532 "If set, this is executed at startup."),
533 SSET (FabAuthor, "", "fab-author", 0),
534 SSET (InitialLayerStack, "", "layer-stack",
535 "Initial layer stackup, for setting up an export."),
537 ISET (PinoutOffsetX, 100, "pinout-offset-x", 0),
538 ISET (PinoutOffsetY, 100, "pinout-offset-y", 0),
539 ISET (PinoutTextOffsetX, 800, "pinout-text-offset-x", 0),
540 ISET (PinoutTextOffsetY, -100, "pinout-text-offset-y", 0),
542 BSET (DrawGrid, 0, "draw-grid", "default to drawing the grid at startup"),
543 BSET (ClearLine, 1, "clear-line", 0),
544 BSET (FullPoly, 0, "full-poly", 0),
545 BSET (UniqueNames, 1, "unique-names", 0),
546 BSET (SnapPin, 1, "snap-pin", 0),
547 BSET (SaveLastCommand, 0, "save-last-command", 0),
548 BSET (SaveInTMP, 0, "save-in-tmp", 0),
549 BSET (AllDirectionLines, 0, "all-direction-lines", 0),
550 BSET (ShowNumber, 0, "show-number", 0),
551 BSET (ResetAfterElement, 1, "reset-after-element", 0),
552 BSET (RingBellWhenFinished, 0, "ring-bell-finished", 0),
555 REGISTER_ATTRIBUTES (main_attribute_list)
556 /* ----------------------------------------------------------------------
557 * post-process settings.
559 static void settings_post_process ()
561 if (Settings.LibraryCommand[0] != PCB_DIR_SEPARATOR_C && Settings.LibraryCommand[0] != '.')
563 Settings.LibraryCommand
565 Concat (Settings.LibraryCommandDir, PCB_DIR_SEPARATOR_S,
566 Settings.LibraryCommand,
567 NULL);
569 if (Settings.LibraryContentsCommand[0] != PCB_DIR_SEPARATOR_C
570 && Settings.LibraryContentsCommand[0] != '.')
572 Settings.LibraryContentsCommand
574 Concat (Settings.LibraryCommandDir, PCB_DIR_SEPARATOR_S,
575 Settings.LibraryContentsCommand, NULL);
578 if (Settings.LineThickness > MAX_LINESIZE
579 || Settings.LineThickness < MIN_LINESIZE)
580 Settings.LineThickness = 1000;
582 if (Settings.ViaThickness > MAX_PINORVIASIZE
583 || Settings.ViaThickness < MIN_PINORVIASIZE)
584 Settings.ViaThickness = 4000;
586 if (Settings.ViaDrillingHole <= 0)
587 Settings.ViaDrillingHole =
588 DEFAULT_DRILLINGHOLE * Settings.ViaThickness / 100;
590 Settings.MaxWidth = MIN (MAX_COORD, MAX (Settings.MaxWidth, MIN_SIZE));
591 Settings.MaxHeight = MIN (MAX_COORD, MAX (Settings.MaxHeight, MIN_SIZE));
593 ParseRouteString (Settings.Routes, &Settings.RouteStyle[0], 1);
597 /* ----------------------------------------------------------------------
598 * Print help or version messages.
601 static void
602 print_version ()
604 printf ("PCB version %s\n", VERSION);
605 exit (0);
608 /* ----------------------------------------------------------------------
609 * Figure out the canonical name of the executed program
610 * and fix up the defaults for various paths
612 char *bindir = NULL;
613 char *exec_prefix = NULL;
614 char *pcblibdir = NULL;
615 char *pcblibpath = NULL;
616 char *pcbtreedir = NULL;
617 char *pcbtreepath = NULL;
618 char *homedir = NULL;
620 static void
621 InitPaths (char *argv0)
623 size_t l;
624 int i;
625 int haspath;
626 char *t1, *t2;
627 int found_bindir = 0;
629 /* see if argv0 has enough of a path to let lrealpath give the
630 * real path. This should be the case if you invoke pcb with
631 * something like /usr/local/bin/pcb or ./pcb or ./foo/pcb
632 * but if you just use pcb and it exists in your path, you'll
633 * just get back pcb again.
636 haspath = 0;
637 for (i = 0; i < strlen (argv0) ; i++)
639 if (argv0[i] == PCB_DIR_SEPARATOR_C)
640 haspath = 1;
643 #ifdef DEBUG
644 printf ("InitPaths (%s): haspath = %d\n", argv0, haspath);
645 #endif
647 if (haspath)
649 bindir = strdup (lrealpath (argv0));
650 found_bindir = 1;
652 else
654 char *path, *p, *tmps;
655 struct stat sb;
656 int r;
658 tmps = getenv ("PATH");
660 if (tmps != NULL)
662 path = strdup (tmps);
664 /* search through the font path for a font file */
665 for (p = strtok (path, PCB_PATH_DELIMETER); p && *p;
666 p = strtok (NULL, PCB_PATH_DELIMETER))
668 #ifdef DEBUG
669 printf ("Looking for %s in %s\n", argv0, p);
670 #endif
671 if ( (tmps = malloc ( (strlen (argv0) + strlen (p) + 2) * sizeof (char))) == NULL )
673 fprintf (stderr, "InitPaths(): malloc failed\n");
674 exit (1);
676 sprintf (tmps, "%s%s%s", p, PCB_DIR_SEPARATOR_S, argv0);
677 r = stat (tmps, &sb);
678 if (r == 0)
680 #ifdef DEBUG
681 printf ("Found it: \"%s\"\n", tmps);
682 #endif
683 bindir = lrealpath (tmps);
684 found_bindir = 1;
685 free (tmps);
686 break;
688 free (tmps);
690 free (path);
694 #ifdef DEBUG
695 printf ("InitPaths(): bindir = \"%s\"\n", bindir);
696 #endif
698 if (found_bindir)
700 /* strip off the executible name leaving only the path */
701 t2 = NULL;
702 t1 = strchr (bindir, PCB_DIR_SEPARATOR_C);
703 while (t1 != NULL && *t1 != '\0')
705 t2 = t1;
706 t1 = strchr (t2 + 1, PCB_DIR_SEPARATOR_C);
708 if (t2 != NULL)
709 *t2 = '\0';
711 #ifdef DEBUG
712 printf ("After stripping off the executible name, we found\n");
713 printf ("bindir = \"%s\"\n", bindir);
714 #endif
716 else
718 /* we have failed to find out anything from argv[0] so fall back to the original
719 * install prefix
721 bindir = strdup (BINDIR);
724 /* now find the path to exec_prefix */
725 l = strlen (bindir) + 1 + strlen (BINDIR_TO_EXECPREFIX) + 1;
726 if ( (exec_prefix = (char *) malloc (l * sizeof (char) )) == NULL )
728 fprintf (stderr, "InitPaths(): malloc failed\n");
729 exit (1);
731 sprintf (exec_prefix, "%s%s%s", bindir, PCB_DIR_SEPARATOR_S,
732 BINDIR_TO_EXECPREFIX);
734 /* now find the path to PCBLIBDIR */
735 l = strlen (bindir) + 1 + strlen (BINDIR_TO_PCBLIBDIR) + 1;
736 if ( (pcblibdir = (char *) malloc (l * sizeof (char) )) == NULL )
738 fprintf (stderr, "InitPaths(): malloc failed\n");
739 exit (1);
741 sprintf (pcblibdir, "%s%s%s", bindir, PCB_DIR_SEPARATOR_S,
742 BINDIR_TO_PCBLIBDIR);
744 /* and the path to PCBTREEDIR */
745 l = strlen (bindir) + 1 + strlen (BINDIR_TO_PCBTREEDIR) + 1;
746 if ( (pcbtreedir = (char *) malloc (l * sizeof (char) )) == NULL )
748 fprintf (stderr, "InitPaths(): malloc failed\n");
749 exit (1);
751 sprintf (pcbtreedir, "%s%s%s", bindir, PCB_DIR_SEPARATOR_S,
752 BINDIR_TO_PCBTREEDIR);
754 /* and the search path including PCBLIBDIR */
755 l = strlen (pcblibdir) + 3;
756 if ( (pcblibpath = (char *) malloc (l * sizeof (char) )) == NULL )
758 fprintf (stderr, "InitPaths(): malloc failed\n");
759 exit (1);
761 sprintf (pcblibpath, ".%s%s", PCB_PATH_DELIMETER, pcblibdir);
763 /* and the newlib search path */
764 l = strlen (pcblibdir) + 1 + strlen (pcbtreedir)
765 + strlen ("pcblib-newlib") + 2;
766 if ( (pcbtreepath = (char *) malloc (l * sizeof (char) )) == NULL )
768 fprintf (stderr, "InitPaths(): malloc failed\n");
769 exit (1);
771 sprintf (pcbtreepath, "%s%s%s%spcblib-newlib", pcbtreedir,
772 PCB_PATH_DELIMETER, pcblibdir,
773 PCB_DIR_SEPARATOR_S);
775 #ifdef DEBUG
776 printf ("bindir = %s\n", bindir);
777 printf ("pcblibdir = %s\n", pcblibdir);
778 printf ("pcblibpath = %s\n", pcblibpath);
779 printf ("pcbtreedir = %s\n", pcbtreedir);
780 printf ("pcbtreepath = %s\n", pcbtreepath);
781 #endif
783 l = sizeof (main_attribute_list) / sizeof (main_attribute_list[0]);
784 for (i = 0; i < l ; i++)
786 if (NSTRCMP (main_attribute_list[i].name, "lib-command-dir") == 0)
788 main_attribute_list[i].default_val.str_value = pcblibdir;
791 if ( (NSTRCMP (main_attribute_list[i].name, "font-path") == 0)
792 || (NSTRCMP (main_attribute_list[i].name, "element-path") == 0)
793 || (NSTRCMP (main_attribute_list[i].name, "lib-path") == 0) )
795 main_attribute_list[i].default_val.str_value = pcblibpath;
798 if (NSTRCMP (main_attribute_list[i].name, "lib-newlib") == 0)
800 main_attribute_list[i].default_val.str_value = pcbtreepath;
806 char *tmps;
808 tmps = getenv ("HOME");
810 if (tmps == NULL) {
811 tmps = getenv ("USERPROFILE");
814 if (tmps != NULL) {
815 homedir = strdup (tmps);
816 } else {
817 homedir = NULL;
823 /* ----------------------------------------------------------------------
824 * main program
827 char *program_name = 0;
828 char *program_basename = 0;
829 char *program_directory = 0;
831 #include "dolists.h"
834 main (int argc, char *argv[])
836 int i;
838 /* init application:
839 * - make program name available for error handlers
840 * - evaluate special options
841 * - initialize toplevel shell and resources
842 * - create an empty PCB with default symbols
843 * - initialize all other widgets
844 * - update screen and get size of drawing area
845 * - evaluate command-line arguments
846 * - register 'call on exit()' function
849 #include "core_lists.h"
850 setbuf (stdout, 0);
851 InitPaths (argv[0]);
853 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
854 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
856 hid_init ();
858 hid_load_settings ();
860 program_name = argv[0];
861 program_basename = strrchr (program_name, PCB_DIR_SEPARATOR_C);
862 if (program_basename)
864 program_directory = strdup (program_name);
865 *strrchr (program_directory, PCB_DIR_SEPARATOR_C) = 0;
866 program_basename++;
868 else
870 program_directory = ".";
871 program_basename = program_name;
873 Progname = program_basename;
875 if (argc > 1 && strcmp (argv[1], "-h") == 0)
876 usage ();
877 if (argc > 1 && strcmp (argv[1], "-V") == 0)
878 print_version ();
879 if (argc > 1 && strcmp (argv[1], "-p") == 0)
881 exporter = gui = hid_find_printer ();
882 argc--;
883 argv++;
885 else if (argc > 2 && strcmp (argv[1], "-x") == 0)
887 exporter = gui = hid_find_exporter (argv[2]);
888 argc -= 2;
889 argv += 2;
891 else
892 gui = hid_find_gui ();
894 if (!gui)
895 exit (1);
897 for (i = 0; i < MAX_LAYER; i++)
899 char buf[20];
900 sprintf (buf, "signal%d", i + 1);
901 Settings.DefaultLayerName[i] = MyStrdup (buf, "DefaultLayerNames");
902 Settings.LayerColor[i] = "#c49350";
903 Settings.LayerSelectedColor[i] = "#00ffff";
906 gui->parse_arguments (&argc, &argv);
908 if (show_help || (argc > 1 && argv[1][0] == '-'))
909 usage ();
910 if (show_version)
911 print_version ();
912 if (show_defaults)
913 print_defaults ();
914 if (show_copyright)
915 copyright ();
917 Output.bgGC = gui->make_gc ();
918 Output.fgGC = gui->make_gc ();
919 Output.pmGC = gui->make_gc ();
920 Output.GridGC = gui->make_gc ();
922 settings_post_process ();
925 if (show_actions)
927 print_actions ();
928 exit (0);
931 if (do_dump_actions)
933 extern void dump_actions (void);
934 dump_actions ();
935 exit (0);
938 PCB = CreateNewPCB (True);
939 PCB->Data->LayerN = DEF_LAYER;
940 ParseGroupString (Settings.Groups, &PCB->LayerGroups, DEF_LAYER);
941 CreateNewPCBPost (PCB, 1);
942 if (argc > 1)
943 command_line_pcb = argv[1];
945 ResetStackAndVisibility ();
947 CreateDefaultFont ();
948 if (gui->gui)
949 InitCrosshair ();
950 InitHandler ();
951 InitBuffers ();
952 SetMode (ARROW_MODE);
954 if (command_line_pcb)
956 /* keep filename even if initial load command failed;
957 * file might not exist
959 if (LoadPCB (command_line_pcb))
960 PCB->Filename = MyStrdup (command_line_pcb, "main()");
963 if (Settings.InitialLayerStack
964 && Settings.InitialLayerStack[0])
966 LayerStringToLayerStack (Settings.InitialLayerStack);
969 if (gui->printer || gui->exporter)
971 gui->do_export (0);
972 exit (0);
975 /* FIX_ME
976 LoadBackgroundImage (Settings.BackgroundImage); */
978 /* Register a function to be called when the program terminates.
979 * This makes sure that data is saved even if LEX/YACC routines
980 * abort the program.
981 * If the OS doesn't have at least one of them,
982 * the critical sections will be handled by parse_l.l
984 atexit (EmergencySave);
986 /* read the library file and display it if it's not empty
988 if (!ReadLibraryContents () && Library.MenuN)
989 hid_action ("LibraryChanged");
991 #ifdef HAVE_LIBSTROKE
992 stroke_init ();
993 #endif
995 * Set this flag to zero. Then if we have a startup
996 * action which includes Quit(), the flag will be set
997 * to -1 and we can avoid ever calling gtk_main();
999 Settings.init_done = 0;
1001 if (Settings.ScriptFilename)
1003 Message (_("Executing startup script file %s\n"),
1004 Settings.ScriptFilename);
1005 hid_actionl ("ExecuteFile", Settings.ScriptFilename, NULL);
1007 if (Settings.ActionString)
1009 Message (_("Executing startup action %s\n"), Settings.ActionString);
1010 hid_parse_actions (Settings.ActionString, 0);
1013 if (Settings.init_done == 0)
1015 Settings.init_done = 1;
1016 #if HAVE_DBUS
1017 pcb_dbus_setup();
1018 #endif
1020 EnableAutosave ();
1022 #ifdef DEBUG
1023 printf ("Settings.LibraryCommandDir = \"%s\"\n",
1024 Settings.LibraryCommandDir);
1025 printf ("Settings.FontPath = \"%s\"\n",
1026 Settings.FontPath);
1027 printf ("Settings.ElementPath = \"%s\"\n",
1028 Settings.ElementPath);
1029 printf ("Settings.LibraryPath = \"%s\"\n",
1030 Settings.LibraryPath);
1031 printf ("Settings.LibraryTree = \"%s\"\n",
1032 Settings.LibraryTree);
1033 #endif
1035 gui->do_export (0);
1036 #if HAVE_DBUS
1037 pcb_dbus_finish();
1038 #endif
1041 return (0);