File: gattrib.c

package info (click to toggle)
geda-gattrib 20061020-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,412 kB
  • ctags: 1,013
  • sloc: ansic: 15,545; sh: 3,549; makefile: 110
file content (392 lines) | stat: -rw-r--r-- 13,820 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* gEDA - GPL Electronic Design Automation
 * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
 * Copyright (C) 2003 Stuart D. Brorson.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
 */

/*****************************************************************************
 * In the spirit of open source/free software, major sections of             *
 * gattrib's code were borrowed from other sources, and hacked               *
 * together by SDB in Dec. 2003.  Particularly rich sources for code were    *
 * gEDA/gnetlist, and the gtkextra program testgtksheet.c.  Thanks to their  *
 * authors for providing the foundation upon which this is built.            *
 *                                                                           *
 * Of course, I *did* write major portions of the code too . . . . .         *
 * Some documentation about the internal operation of this program can be    *
 * found in the "NOTES" file  in the gattrib top-level directory.            *
 * -- SDB  December 2003 -                                                   *
 *****************************************************************************/

#include <config.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

/*------------------------------------------------------------------
 * Includes originally from testgtksheet -- stuff needed to deal with 
 * spreadsheet widget.
 *------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

#include <glib.h>
#include <glib-object.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif

/*------------------------------------------------------------------
 * Gattrib specific includes -- stuff dealing with gattrib data structs.
 *------------------------------------------------------------------*/
#include <libgeda/libgeda.h>       /* geda library fcns  */
#include "../include/struct.h"     /* typdef and struct declarations */
#include "../include/prototype.h"  /* function prototypes */
#include "../include/globals.h"

#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif

/*------------------------------------------------------------------
 * gattrib_really_quit callback -- called when user selects "quit"
 * from menubar.  Checks for unsaved changes.
 *------------------------------------------------------------------*/
void gattrib_really_quit(void)
{
  if (sheet_head->CHANGED == TRUE) {
    printf("User is quitting without saving last changes.\n");
    x_dialog_unsaved_data();
  } else {
    gattrib_quit(0);
  }
}


/*------------------------------------------------------------------
 * gattrib_quit -- wrap up and quit fcn.
 *------------------------------------------------------------------*/
gint gattrib_quit(gint return_code)
{
  /*   s_clib_cache_free(); */
  s_clib_free();
  s_slib_free();
  /* s_rename_destroy_all(); */
#ifdef DEBUG
  fflush(stderr);
  fflush(stdout);
  printf("In gattrib_quit, calling gtk_main_quit()\n");
#endif
  gtk_main_quit();
  exit(return_code);
}


/*------------------------------------------------------------------
 * gattrib_main -- main gattrib fcn.
 *------------------------------------------------------------------*/
void gattrib_main(void *closure, int argc, char *argv[])
{
  /* TOPLEVEL *pr_current is a global */
  /* SHEET_DATA *sheet_head is a global */
  /* GtkWidget *main_window is a global */

  int i;
  int return_code;  /* used when invoking s_toplevel_read_page */
  int argv_index;
  char *cwd;
  PAGE *p_local;
  char *logfile;
  

  
  /* Initialize gEDA stuff */
  libgeda_init();

  /* Note that argv_index holds index to first non-flag command line option 
   * (that is, to the first file name) */
  argv_index = parse_commandline(argc, argv);
  cwd = g_strdup(getcwd(NULL, 1024));
  
  /* ----------  create log file right away ---------- */
  /* ----------  even if logging is enabled ---------- */
  logfile = g_build_path (G_DIR_SEPARATOR_S,
                          cwd,
                          "gattrib.log",
                          NULL);
  s_log_init (logfile);
  g_free (logfile);
  
  s_log_message
    ("gEDA/gattrib version %s%s\n", VERSION, CUSTOM_VERSION);
  s_log_message
    ("gEDA/gattrib comes with ABSOLUTELY NO WARRANTY; see COPYING for more details.\n");
  s_log_message
    ("This is free software, and you are welcome to redistribute it under certain\n");
  s_log_message
    ("conditions; please see the COPYING file for more details.\n\n");
  
  if (!quiet_mode) {
    fflush(stderr);
    fflush(stdout);
    fprintf(stderr, 
	    "gEDA/gattrib version %s%s\n", VERSION, CUSTOM_VERSION);
    fprintf(stderr,
	    "gEDA/gattrib comes with ABSOLUTELY NO WARRANTY; see COPYING for more details.\n");
    fprintf(stderr,
	    "This is free software, and you are welcome to redistribute it under certain\n");
    fprintf(stderr,
	    "conditions; please see the COPYING file for more details.\n\n");
  }
  

  /* ------  register guile (scheme) functions.  Necessary to parse RC file.  ------ */
  g_register_funcs();

  /* ---------- Start creation of new project: (TOPLEVEL *pr_current) ---------- */
  pr_current = s_toplevel_new();

  /* ----- Read in RC files.   ----- */
  g_rc_parse(pr_current, "gattribrc", rc_filename);

  /*
  i_vars_set(pr_current);
  */

  i_window_vars_set(pr_current);   /* The window vars are used in gschem,
                                      but we need to set them here because
                                      graphical information is used
                                      when introducing new attributes. */

#if DEBUG
  printf("In gattrib_main -- we have just created and init'ed a new pr_current\n");
#endif  



  /* --------  Initialize main_window.  -------- */
#if DEBUG
  printf("In gattrib_main -- calling gtk_init. . . ..\n");
#endif  
  gtk_init(&argc, &argv);

  x_window_init();  
#if DEBUG
  printf("In gattrib_main -- we have just initialized the main_window.\n");
#endif  
 
  
  /* ---------- Initialize SHEET_DATA data structure ---------- */
  sheet_head = s_sheet_data_new();   /* sheet_head was declared in globals.h */


  /* ----- Now loop on the files specified on the cmd line & read them in ---- */
  /* argv[0] = name of this prog (gattrib).  argv_index holds the 
   * position of the first filename  */
  i = argv_index;
  while(argv[i] != NULL) {

    gchar *temp_filename;
    gchar *filename;
#ifdef __MINGW32__
    if (argv[i][1] == ':' && (argv[i][2] == G_DIR_SEPARATOR ||
                              argv[i][2] == OTHER_PATH_SEPARATER_CHAR))
#else
    if (argv[i][0] == G_DIR_SEPARATOR)
#endif
    {
      /* Path is already absolute so no need to do any concat of cwd */
      temp_filename = g_strdup(argv[i]);
    } else {
      temp_filename = g_build_path (G_DIR_SEPARATOR_S, cwd, argv[i], NULL);
    }

    filename = f_normalize_filename(temp_filename);
    g_free(temp_filename);

    s_log_message("Loading file [%s]\n", filename);
    if (!quiet_mode) {
      printf ("Loading file [%s]\n", filename);
    }

    s_page_goto (pr_current,
		 s_page_new (pr_current, filename));
    
    return_code = 0;
    if (first_page == 1) {
      return_code |= s_toplevel_read_page(filename);
      first_page = 0;
    } else {
      return_code |= s_toplevel_read_page(filename);
    }
    
    /* Now add all items found to the master lists */
    s_sheet_data_add_master_comp_list_items(pr_current->page_current->object_head); 
    s_sheet_data_add_master_comp_attrib_list_items(pr_current->page_current->object_head); 
    
#if 0
    /* Note that this must be changed.  We need to input the entire project
     * before doing anything with the nets because we need to first
     * determine where they are all connected!   */
    s_sheet_data_add_master_net_list_items(pr_current->page_current->object_head);    
    s_sheet_data_add_master_net_attrib_list_items(pr_current->page_current->object_head); 
#endif

    s_sheet_data_add_master_pin_list_items(pr_current->page_current->object_head); 
    s_sheet_data_add_master_pin_attrib_list_items(pr_current->page_current->object_head); 

    i++;
    g_free(filename);
  }  /* while(argv[i])  */
  g_free(cwd); /* cwd is allocated with g_strdup so, g_free is correct */


  /* ---------- Now complete read-in of project  ---------- */
  if ( first_page != 1 ) { 


    /* ---------- Sort the master lists  ---------- */
    s_string_list_sort_master_comp_list();
    s_string_list_sort_master_comp_attrib_list();

#if 0
    /* Note that this must be changed.  We need to input the entire project
     * before doing anything with the nets because we need to first
     * determine where they are all connected!   */
    s_string_list_sort_master_net_list();
    s_string_list_sort_master_net_attrib_list();
#endif

    s_string_list_sort_master_pin_list();
    s_string_list_sort_master_pin_attrib_list();

    /* ---------- Create and load the tables  ---------- */
    sheet_head->component_table = s_table_new(sheet_head->comp_count, sheet_head->comp_attrib_count);
    sheet_head->net_table = s_table_new(sheet_head->net_count, sheet_head->net_attrib_count);
    sheet_head->pin_table = s_table_new(sheet_head->pin_count, sheet_head->pin_attrib_count);
    
    p_local = pr_current->page_head; /* must iterate over all pages in design */
    while (p_local != NULL) {
      if (p_local->pid != -1) {   /* only traverse pages which are toplevel */
	if (p_local->object_head && p_local->page_control == 0) {
	  s_table_add_toplevel_comp_items_to_comp_table(p_local->object_head);    /* adds all components from page to comp_table */

#if 0
	  /* Note that this must be changed.  We need to input the entire project
	   * before doing anything with the nets because we need to first
	   * determine where they are all connected!   */
	  s_table_add_toplevel_net_items_to_net_table(p_local->object_head);     /* adds all nets from page to net_table */
#endif

	  s_table_add_toplevel_pin_items_to_pin_table(p_local->object_head);    /* adds all pins from page to pin_table */

	}
      }
      p_local = p_local->next;  /* iterate to next schematic page */
    }
#if DEBUG
    printf("In gattrib_main -- we have just returned from adding to the tables.\n");
#endif  


#if DEBUG
    /*  -----  Make debug printout of entire object list  -----  */
    printf("In gattrib_main -- we have just read in the project and filled out pr_current\n");
    printf("----------------------------  Object list -----------------------------\n");
    s_page_print_all(pr_current);
    printf("-----------------------------------------------------------------------\n");
#endif

    
    /* -------------- Next, update windows --------------- */
    x_window_add_items();    /* This updates the top level stuff,
			      * and then calls another fcn to update
			      * the GtkSheet itself.  */
#if DEBUG
    printf("In gattrib_main -- we have just returned from x_window_add_items.\n");
#endif  
    
  }  /* if (first_page != 1) */
  else {
    /* no filename found on command line, therefore we are still on the first page */
#if DEBUG
    printf("In gattrib_main -- no files specified on command line.  Throw up filedialog.\n");
#endif
    x_fileselect_open ();

    gtk_widget_show( GTK_WIDGET(notebook) );
    gtk_widget_show( GTK_WIDGET(window) );

    while( gtk_events_pending () ) {
#ifdef DEBUG
      printf("In gattrib_main, trying to flush gtk event queue before running gtk_main. . . . \n");
#endif
      gtk_main_iteration();  /* force window exposure by running event handler once */
    }
    
  }

  /* ---------- Now verify correctness of read-in design.  ---------- */
  s_toplevel_verify_design(pr_current);


  /* ---------- Now enter main event loop for spreadsheet.  ---------- */
  gtk_widget_show( GTK_WIDGET(window) );  /*  One final show for good measure  */
  gtk_main_iteration();  /* force window exposure by running event handler once */
  gtk_main();


  /* ---------- Spreadsheet has been killed; we are quitting.  ---------- */
#ifdef DEBUG
  printf("In gattrib_main, we have exited gtk_main. \n");
#endif  

  exit(0);
}

/*------------------------------------------------------------------
 * main -- entry point to gattrib.  This is just a wrapper which 
 * invokes the guile stuff, and points to the real main prog, 
 * gattrib_main.  Note that I still need some vestigal
 * guile stuff in order to read the rc files.
 *------------------------------------------------------------------*/
int main(int argc, char *argv[])
{

  /* This is i18n stuff */
#if ENABLE_NLS
  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);
  bind_textdomain_codeset(PACKAGE, "UTF-8");
#endif

  /* disable the deprecated warnings in guile 1.6.3 */
  /* Eventually the warnings will need to be fixed */
  if(getenv("GUILE_WARN_DEPRECATED")==NULL)
    putenv("GUILE_WARN_DEPRECATED=no");
  
  scm_boot_guile( argc, argv, gattrib_main, NULL);

#ifdef DEBUG
  printf("Now exiting main . . . Bye!\n");
#endif
  exit(0);   /* This is not real exit point.  Real exit is in gattrib_quit. */
}