File: s_basic.nw

package info (click to toggle)
libgeda 20050313-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,636 kB
  • ctags: 1,505
  • sloc: ansic: 19,449; sh: 8,627; makefile: 197; perl: 59
file content (1093 lines) | stat: -rw-r--r-- 23,239 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
@c -*- mode: Noweb; noweb-doc-mode: texinfo-mode; noweb-code-mode: c-mode -*-

@node File s_basic.c,,,Top
@chapter File @file{s_basic.c}

@section File header

<<s_basic.c : *>>=
<<s_basic.c : copyright and license>>

/* DO NOT read or edit this file ! Use ../noweb/s_basic.nw instead */

<<s_basic.c : include directives>>
<<s_basic.c : global variables>>
<<s_basic.c : error_if_called()>>
<<s_basic.c : error_if_null()>>
<<s_basic.c : return_tail()>>
<<s_basic.c : return_head()>>
<<s_basic.c : s_basic_init_object()>>
<<s_basic.c : s_basic_link_object()>>
<<s_basic.c : print_struct_forw()>>
<<s_basic.c : print_struct_back()>>
<<s_basic.c : print_struct()>>
<<s_basic.c : s_delete()>>
<<s_basic.c : s_delete_list_fromstart()>>
<<s_basic.c : s_remove()>>                /* Done */
<<s_basic.c : string_toupper()>>
<<s_basic.c : string_tolower()>>
<<s_basic.c : colornametovalue()>>
<<s_basic.c : remove_nl()>>
<<s_basic.c : remove_last_nl()>>

#ifndef HAVE_VSNPRINTF
<<s_basic.c : vsnprintf()>>
#endif

<<s_basic.c : remove_string()>>
<<s_basic.c : insert_string()>>
<<s_basic.c : expand_env_variables()>>


@ 


<<s_basic.c : copyright and license>>=
/* gEDA - GPL Electronic Design Automation
 * libgeda - gEDA's library
 * Copyright (C) 1998-2000 Ales V. Hvezda
 *
 * 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
 */

@ 


<<s_basic.c : include directives>>=
#include <config.h>

#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#ifndef HAVE_VSNPRINTF
#include <stdarg.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <gtk/gtk.h>
#include <libguile.h>

#include "defines.h"
#include "struct.h"
#include "defines.h"
#include "globals.h"
#include "o_types.h"
#include "colors.h"

#include "../include/prototype.h"

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

@ 


<<s_basic.c : global variables>>=
/* this is modified here and in o_list.c */
int global_sid=0;

#define NUMCOLORS 9

struct st_old_colors {
        char *name;
        int value;
};

/* Colors must be in alphabetical order */
/* be sure that you update above define */
struct st_old_colors old_colors[] = {
	{ "black", 0 },
	{ "blue", 4 },
	{ "cyan", 6 },
	{ "green", 3 },
	{ "grey", 7 }, 
	{ "grey90", 8 },
	{ "red", 2 },
	{ "white", 1 },
	{ "yellow", 5 },
};

@ %def global_sid NUMCOLORS old_colors 


@section Function @code{error_if_called()}

@defun error_if_called
@end defun

<<s_basic.c : error_if_called()>>=
void error_if_called(void)
{
	fprintf(stderr, "Somebody called error_if_called!\n");
	assert(0);
}

@ %def error_if_called


@section Function @code{error_if_null()}

@defun error_if_null
@end defun

<<s_basic.c : error_if_null()>>=
void exit_if_null(void *ptr) 
{
  if (ptr == NULL) {
    fprintf(stderr, "gEDA: Got NULL ptr!, please e-mail maintainer\n");
    assert(0);
    exit(-1);
  }	
}

@ %def exit_if_null


@section Function @code{return_tail()}

@defun return_tail head
@end defun

<<s_basic.c : return_tail()>>=
/* hack rename this to be s_return_tail */
/* update object_tail or any list of that matter */
OBJECT *
return_tail(OBJECT *head)
{
  OBJECT *o_current=NULL;
  OBJECT *ret_struct=NULL;

  o_current = head;
  while ( o_current != NULL ) { /* goto end of list */
    ret_struct = o_current;	
    o_current = o_current->next;
  }
	
  return(ret_struct);	
}

@ %def return_tail


@section Function @code{return_head()}

@defun return_head tail
@end defun

<<s_basic.c : return_head()>>=
/* hack rename this to be s_return_head */
/* update object_tail or any list of that matter */
OBJECT *
return_head(OBJECT *tail)
{
  OBJECT *o_current=NULL;
  OBJECT *ret_struct=NULL;

  o_current = tail;
  while ( o_current != NULL ) { /* goto end of list */
    ret_struct = o_current;	
    o_current = o_current->prev;
  }
	
  return(ret_struct);	
}

@ %def return_head


@section Function @code{s_basic_init_object()}

@defun s_basic_init_object name
@end defun

<<s_basic.c : s_basic_init_object()>>=
OBJECT *
s_basic_init_object( char *name ) 
{
  OBJECT *new_node;

  new_node = (OBJECT *) malloc(sizeof(OBJECT));	

  if (new_node == NULL) {
    fprintf(stderr, "Could not perform malloc; something is broken or increase your process limits\n");
    exit(-1);
  }

  /* setup sid */
  new_node->sid = global_sid++;
  new_node->type = -1;

  /* Setup the name */
  /* TODO: get rid of magic number 16 that's the size of new_node->sid, */
  new_node->name = (char *) malloc(sizeof(char)*(strlen(name)+16));
  sprintf(new_node->name, "%s.%d", name, new_node->sid);

  /* Setup the bounding box */
  new_node->top = 999999;
  new_node->left = 999999;
  new_node->right = 0;
  new_node->bottom = 0;

  /* Setup line/circle structs */
  new_node->line = NULL;
  new_node->circle = NULL;
  new_node->arc = NULL;
  new_node->box = NULL;
  new_node->picture = NULL;
  new_node->text = NULL;
  new_node->complex = NULL;

  new_node->tile_locs = NULL;

  new_node->conn_list = NULL;

  new_node->visited = 0;
	
  new_node->complex_basename = NULL;
  new_node->complex_clib = NULL;
  new_node->complex_parent = NULL;
		
  /* Setup the color */
  new_node->color = WHITE;
  new_node->saved_color = -1;
  new_node->selected = FALSE;
  new_node->locked_color = -1;
  new_node->draw_grips = FALSE;

  new_node->bus_ripper_direction = 0;

  new_node->action_func = error_if_called; 
  new_node->sel_func = error_if_called; 
  new_node->draw_func = error_if_called; 

  new_node->line_end = END_NONE;
  new_node->line_type = TYPE_SOLID;
  new_node->line_width = 0;
  new_node->screen_line_width = 0;
  new_node->line_space = 0;
  new_node->screen_line_space = 0;
  new_node->line_length = 0;
  new_node->screen_line_length = 0;
  new_node->fill_width = 0;
  new_node->screen_fill_width = 0;
  new_node->fill_angle1 = 0;
  new_node->fill_angle2 = 0;
  new_node->fill_pitch1 = 0;
  new_node->screen_fill_pitch1 = 0;
  new_node->fill_pitch2 = 0;
  new_node->screen_fill_pitch2 = 0;
	
  new_node->attribs = NULL;
  new_node->attached_to = NULL;
  new_node->attribute = 0; 
  new_node->show_name_value = SHOW_NAME_VALUE;
  new_node->visibility = VISIBLE;

  new_node->pin_type = PIN_TYPE_NET;
  new_node->whichend = -1;
	
  /* Setup link list stuff */
  new_node->prev = NULL;
  new_node->next = NULL;

  return(new_node);
}

@ %def s_basic_init_object


@section Function @code{s_basic_link_object()}

@defun s_basic_link_object new_node ptr
@end defun

<<s_basic.c : s_basic_link_object()>>=
OBJECT *
s_basic_link_object( OBJECT *new_node, OBJECT *ptr ) 
{
  /* should never happen, but could */
  if (new_node == NULL) {
    fprintf(stderr, "Got a null new_node in link_object\n");
    return(ptr);
  }

  if (ptr == NULL) {
    new_node->prev = NULL; /* setup previous link */
    return(new_node);
  } else {
    new_node->prev = ptr; /* setup previous link */
    ptr->next = new_node;
    return(ptr->next);
  }
}

@ %def s_basic_link_object


@section Function @code{print_struct_forw()}

@defun print_struct_forw ptr
@end defun

<<s_basic.c : print_struct_forw()>>=
void
print_struct_forw(OBJECT *ptr)
{
  OBJECT *o_current=NULL;
  ATTRIB *attr=NULL;
  int i;

  o_current = ptr;

  if (o_current == NULL) {

    printf("AGGGGGGGGGGG NULLLLL PRINT\n");
  }
  printf("TRYING to PRINT\n");
  while (o_current != NULL) {
    printf("Name: %s\n", o_current->name);
    printf("Type: %d\n", o_current->type);
    printf("Sid: %d\n", o_current->sid);

    if (o_current->type == OBJ_COMPLEX || o_current->type == OBJ_PLACEHOLDER) {
      print_struct_forw(o_current->complex->prim_objs);
    }

	
    if (o_current->attribs) {
      attr = o_current->attribs;
      i = 0;
      while (attr != NULL) {
        if (attr->object != NULL) 
          printf("%d attribute %s\n", i, attr->object->name);
        attr = attr->next;
      }    
    }

    printf("----\n");
    o_current = o_current->next;
  }
}

@ %def print_struct_forw


@section Function @code{print_struct_back()}

@defun print_struct_back ptr
@end defun

<<s_basic.c : print_struct_back()>>=
void
print_struct_back(OBJECT *ptr)
{
  OBJECT *o_current=NULL;

  o_current = ptr;

  while (o_current != NULL) {
    printf("Name: %s\n", o_current->name);
    printf("Type: %d\n", o_current->type);
    printf("Sid: %d\n", o_current->sid);
    printf("----\n");
    o_current = o_current->prev;
  }
}

@ %def print_struct_back


@section Function @code{print_struct()}

@defun print_struct ptr
@end defun

<<s_basic.c : print_struct()>>=
void
print_struct(OBJECT *ptr)
{
  OBJECT *o_current=NULL;
  ATTRIB *attr=NULL;
  int i;

  o_current = ptr;

  if (o_current != NULL) {
    printf("Name: %s\n", o_current->name);
    printf("Type: %d\n", o_current->type);
    printf("Sid: %d\n", o_current->sid);
    if (o_current->line != NULL) {
      printf("Line points.x1: %d\n", o_current->line->x[0]);
      printf("Line points.y1: %d\n", o_current->line->y[0]);
      printf("Line points.x2: %d\n", o_current->line->x[1]);
      printf("Line points.y2: %d\n", o_current->line->y[1]);
    }

    if (o_current->attribs) {
      attr = o_current->attribs;
      i = 0;
      while (attr != NULL) {
        printf("%d attribute %s\n", i, attr->object->name);
        attr = attr->next;
      }
		
    }
    printf("----\n");
  }
}

@ %def print_struct


@section Function @code{s_delete()}

@defun s_delete w_current o_current
@end defun

<<s_basic.c : s_delete()>>=
void
s_delete(TOPLEVEL *w_current, OBJECT *o_current)
{
  if (o_current != NULL) {


#if DEBUG
    printf("sdel: %s\n", o_current->name);
    printf("sdel: %d\n", o_current->sid);
#endif

    if (o_current->next) 
    o_current->next->prev = o_current->prev;
    else
    o_current->next = NULL;

    if (o_current->prev) 
    o_current->prev->next = o_current->next;
    else
    o_current->prev = NULL;

    s_conn_remove(w_current, o_current);
	
    /* second half of if is odd that we need it? hack */
    /* need to do this early so we can do the printfs */
    if (o_current->attached_to != NULL && o_current->attribute == 1) {
      if (o_current->attached_to->object) {
				/*printf("removing %s\n", o_current->attached_to->object->name);*/
      } else {
        printf("found a null I didn't expect!!!!!!!!!\n");
      }

      /* do the actual remove */
      o_attrib_delete(o_current->attached_to);
    }

    if (w_current->page_current->object_lastplace == o_current) {
      w_current->page_current->object_lastplace = NULL; 
    }

    if (o_current->line) {
      /*	printf("sdeleting line\n");*/
      free(o_current->line);

      /* yes this object might be in the tile system */
      s_tile_remove_object_all(w_current,
                               w_current->page_current,
                               o_current);
    }
    o_current->line = NULL;

    if (o_current->circle) {
      /*	printf("sdeleting circle\n");*/
      free(o_current->circle);
    }
    o_current->circle = NULL;

    if (o_current->arc) {
      /*	printf("sdeleting arc\n");*/
      free(o_current->arc);
    }
    o_current->arc = NULL;

    if (o_current->box) {
      /*	printf("sdeleting box\n");*/
      free(o_current->box);
    }
    o_current->box = NULL;

    if (o_current->picture) {
      /*	printf("sdeleting picture\n");*/
#ifndef HAS_GTK12
      if (o_current->picture->original_picture)
	free(o_current->picture->original_picture);
      if (o_current->picture->displayed_picture)
	free(o_current->picture->displayed_picture);
#endif
      if (o_current->picture->filename)
	free(o_current->picture->filename);
      free(o_current->picture);
    }
    o_current->picture = NULL;

    if (o_current->text) {
      if (o_current->text->string) {
				/*printf("sdeleting text->string\n");*/
        free(o_current->text->string); 
      }
      o_current->text->string = NULL;

      if (o_current->text->prim_objs) {
				/*printf("sdeleting text complex\n");*/
        s_delete_list_fromstart(w_current, 
                                o_current->text->prim_objs);
      }
      o_current->text->prim_objs = NULL;

      /*	printf("sdeleting text\n");*/
      free(o_current->text);
    }
    o_current->text = NULL;

    if (o_current->name) {
      /*	printf("sdeleting name\n");*/
      free(o_current->name);
    }
    o_current->name = NULL;


    if (o_current->complex_basename) {
      /*	printf("sdeleting complex_basename\n");*/
      free(o_current->complex_basename); 
    }
    o_current->complex_basename = NULL;

    if (o_current->complex_clib) {
      /*	printf("sdeleting complex_clib\n");*/
      free(o_current->complex_clib); 
    }
    o_current->complex_clib = NULL;

    if (o_current->complex) {

      if (o_current->complex->prim_objs) {
        /* printf("sdeleting complex->primitive_objects\n");*/
        s_delete_list_fromstart(w_current, 
                                o_current->complex->prim_objs);
      }
      o_current->complex->prim_objs = NULL;

      free(o_current->complex);
      o_current->complex = NULL;
    }

    if (o_current->attribs) {
      o_attrib_free_all(w_current, o_current->attribs);
    }
    o_current->attribs = NULL;


    free(o_current);	/* assuming it is not null */

    o_current=NULL;		/* misc clean up */
  }
}

@ %def s_delete


@section Function @code{s_delete_list_fromstart()}

@defun s_delete_list_fromstart w_current start
@end defun

<<s_basic.c : s_delete_list_fromstart()>>=
/* deletes everything include the head */
void
s_delete_list_fromstart(TOPLEVEL *w_current, OBJECT *start)
{
  OBJECT *temp=NULL; /* literally is a temp */
  OBJECT *current=NULL; /* ugg... you have both o_current and current? */
  OBJECT *o_current=NULL; /* hack */

  temp = start;
  current = return_tail(start);

  /* do the delete backwards */
  /*while(current != NULL && current->type != OBJ_HEAD ) {*/
  while(current != NULL) {
    o_current = current->prev;
    s_delete(w_current, current);
    current = o_current;
  }

  /* now delete the head node */
  /* might not need this but what the hell */
  /* no longer needed, since it's deleted above */
  /*s_delete_head(w_current, start);*/
}

#if 0 /* old way of doing this */
s_delete_list_fromstart(OBJECT *start)
{
  OBJECT *traverse=NULL;
  OBJECT *o_current=NULL;

  for (traverse = start; traverse ; traverse = o_current) {
    o_current = traverse->next;
    if (traverse->type != OBJ_HEAD) /* don't delete any head nodes */
      s_delete(traverse);
    else 
      break; /* found a head node */
  }
  s_delete(traverse);
}
#endif

@ %def delete_list_fromstart


@section Function @code{s_remove()}

@defun s_remove object
This function removes one object pointed by parameter [[object]] from a list as far as it does not represents a head. If so the function returns NULL. If not it returns the pointer on the object, i.e. the same as the parameter.
@end defun

This function must be followed by a call to [[return_tail()]] on the list it belonged to as it can be the last object. Therefore the tail of the list is modified.

<<s_basic.c : s_remove()>>=
OBJECT *
s_remove(TOPLEVEL *w_current, OBJECT *object)
{
  if(object->type == OBJ_HEAD)
  return NULL;
	
  if(object->prev != NULL)
  object->prev->next = object->next;
  if(object->next != NULL)
  object->next->prev = object->prev;

  object->next = NULL;
  object->prev = NULL;

  return object;
}

@ %def s_remove


@section Function @code{string_toupper()}

@defun string_toupper in out
@end defun

<<s_basic.c : string_toupper()>>=
void
string_toupper(char *in, char *out)
{
  int len;
  int i;

  len = strlen(in);

  for (i = 0 ; i < len ; i++) {
    out[i] = toupper(in[i]);
  }
}

@ %def string_toupper


@section Function @code{string_tolower()}

@defun string_tolower in out
@end defun

<<s_basic.c : string_tolower()>>=
void
string_tolower(char *in, char *out)
{
	int len;
	int i;

	len = strlen(in);

	for (i = 0 ; i < len ; i++) {
		out[i] = tolower(in[i]);
	}
}

@ %def string_tolower


@section Function @code{colornametovalue()}

@defun colornametovalue string
@end defun

<<s_basic.c : colornametovalue()>>=
int
colornametovalue(char *string)
{
	
  int lower = 0; 
  int upper = NUMCOLORS - 1;
  int middle;
  int val;
  struct st_old_colors *ptr=NULL;

  if (!string) {
    return(-1);
  }

  string_tolower(string, string);
  while (lower <= upper) {
    middle = (lower + upper) / 2;

    ptr = &old_colors[middle];
    val = strcmp (ptr->name, string);

    if (val < 0) {
      lower = middle + 1;
    } else if (val == 0) {
      return(ptr->value);
    } else {
      upper = middle - 1;
    }
  }                
  return(-1);
}

@ %def colornametovalue


@section Function @code{remove_nl()}

@defun remove_nl string
@end defun

<<s_basic.c : remove_nl()>>=
/* used by o_text_read */
char *
remove_nl(char *string)
{
  int i;

  if (!string)
    return NULL;
  
  i = 0;
  while(string[i] != '\0' && string[i] != '\n' && string[i] != '\r') {
    i++; 
  }

  string[i] = '\0';

  return(string);
}

@ %def remove_nl


@section Function @code{remove_nl()}

@defun remove_last_nl string
@end defun

<<s_basic.c : remove_last_nl()>>=
/* used by o_text_read */
char *
remove_last_nl(char *string)
{
  int len;

  if (!string)
    return NULL;		

  len = strlen(string);
  if (string[len-1] == '\n' || string[len-1] == '\r')
    string[len-1] = '\0';
     
  return(string);
}

@ %def remove_last_nl


@section Function @code{vsnprintf()}

@defun vsnprintf buff bufsiz fmt ap
@end defun

<<s_basic.c : vsnprintf()>>=
void vsnprintf(char *buff, size_t bufsiz, const char *fmt, va_list ap)
{
    char *tmpbuf = buff;

    vsprintf(tmpbuf, fmt, ap);
}


@ %def vsnprintf


@section Function @code{remove_string()}

@defun remove_string string start end
@end defun

<<s_basic.c : remove_string()>>=
/* this function is called by expand_env_variables */
/* changes and returns new string, frees the one that was passed in */
char *
remove_string(char *string, int start, int end) 
{
  char *return_string;
  int i;
  int len;
  int j;

  if (!string) {
    return(NULL);
  }

  len = strlen(string);

  return_string = (char *) malloc(sizeof(char)*(len+1));

  j = 0;
  for (i = 0 ; i < len; i++) {
    if (i >= start && i <= end) {
      /* do nothing */
      /* removing characters */
    } else {
      return_string[j] = string[i];
      j++;
    }
  }
  return_string[j] = '\0';

  /* free original string */
  free(string);

  return(return_string);
}

@ %def remove_string


@section Function @code{insert_string()}

@defun insert_string string start insert_string
@end defun

<<s_basic.c : insert_string()>>=
/* this function is called by expand_env_variables */
/* changes and returns new string, frees the one that was passed in */
char *
insert_string(char *string, int start, char *insert_string)
{
  char *new_string=NULL;
  int i;
  int len;
  int insert_len;
  int total_len;
  int j;
  int orig_count=0;

  /* this should never happen */
  if (!insert_string) {
    return(NULL);	
  }

  /* this should never happen either */
  if (!string) {
    return(NULL);	
  }

  len = strlen(string);
  insert_len = strlen(insert_string);
  total_len = len+insert_len;

  new_string = (char *) malloc(sizeof(char)*(total_len+1));

  i = 0;
  while (i < total_len) {
    if (i == start) {
      for (j = 0 ; j < insert_len; j++) {
        new_string[i+j] = insert_string[j];
      }
      i = j+i;
    } else {
      new_string[i] = string[orig_count];
      i++;
      orig_count++;
    }
  }

  new_string[i] = '\0';

  /* now free the original string */
  free(string);

  return(new_string);
}

@ %def insert_string


@section Function @code{expand_env_variables()}

@defun expand_env_variables string
@end defun

<<s_basic.c : expand_env_variables()>>=
/* this function changes and then returns the string which has the 
 * expanded environment variables, frees passed in string */
/* Environment variables MUST be in the form ${variable_name} */
/* $variable_name is not valid here */
char *
expand_env_variables(char *string)
{
  char wanted_var[80]; /* size is hack */
  char *return_string=NULL;
  char *environment_string=NULL;
  int changed=1;
  int found_dollar=0;
  int found_lbrac=0; 
  int found_rbrac=0;
  int start_of_variable= -1;
  int end_of_variable= -1;
  int count=0;
  int i,j;

  if (!string) {
    return(NULL);
  }

  return_string = string;	

  while(changed) {
	
    changed=0;
    j=0;
    for (i = 0 ; i < strlen(return_string); i++) {

      switch(return_string[i]) {

        case('$'):

#if DEBUG
          printf("found a $\n");
#endif
          found_dollar=1;	
          start_of_variable=i;
          break;

        case('{'):
          if (found_dollar) {
            found_lbrac=1;
            count=1;
          }
          break;

        case('}'):
          if (found_dollar) {
            found_rbrac=1;
            /* ends filling of wanted_var */	
            found_lbrac=0;
            end_of_variable=i;
          }
          break;

      }

      /* the > 1 bit is so that we don't store the { */
      if (found_dollar && found_lbrac && (count > 1)) {
        wanted_var[j] = return_string[i];
        j++; /* check for size */
      }

      /* skip over initial { */ 
      count++;

      if (found_rbrac && !found_lbrac) {
        wanted_var[j] = '\0';
#if DEBUG
        printf("variable wanted: _%s_\n",  wanted_var);
        printf("Between index: %d and %d\n", 
               start_of_variable,
               end_of_variable);
#endif

		
        environment_string = getenv(wanted_var);	

#if DEBUG
        if (environment_string) {
          printf("%s = _%s_\n", wanted_var, 
                 environment_string);
        }
#endif

        return_string = remove_string(return_string,
                                      start_of_variable,
                                      end_of_variable);

#if DEBUG
        printf("removed string: _%s_\n", return_string);
#endif

        if (environment_string) {
          return_string = insert_string(
                                        return_string,
                                        start_of_variable,
                                        environment_string);

        }

#if DEBUG
        printf("final string: _%s_\n", return_string);
#endif
        changed=1;

				/* end of search */
        found_dollar=0;
        found_rbrac=0;
        count=0;
        start_of_variable= -1;
        end_of_variable= -1;

        break;
      }
    }
  }

  if (found_dollar) {
    fprintf(stderr, "Found malformed environment variable (use ${varname})!\n");
  }

  return(return_string);
}

@ %def expand_env_variables