]> git.sur5r.net Git - cc65/blob - src/cc65/asmline.c
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / src / cc65 / asmline.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asmline.h                                 */
4 /*                                                                           */
5 /*                     Internal assembler line structure                     */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37
38 #include "error.h"
39 #include "mem.h"
40 #include "asmline.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Data                                    */
46 /*****************************************************************************/
47
48
49
50 /* Number used to index lines */
51 static unsigned long LineIndex = 0;
52
53 /* The line list */
54 Line*  FirstLine = 0;           /* Pointer to first line */
55 Line*  LastLine  = 0;           /* Pointer to last line */
56
57
58
59 /*****************************************************************************/
60 /*                                   Code                                    */
61 /*****************************************************************************/
62
63
64
65 static Line* NewLine (const char* Format, va_list ap)
66 /* Interal routine to create a new line from the given text */
67 {
68     char        Buf [8192];
69     int         OVF;
70     unsigned    Len;
71     Line*       L;
72
73
74     /* Make a string from the given format and arguments */
75 #if defined(__WATCOMC__)
76     OVF = (_vbprintf (Buf, sizeof (Buf), Format, ap) >= sizeof (S));
77 #else
78     /* Assume gcc running on a Unix OS */
79     OVF = (vsnprintf (Buf, sizeof (Buf), Format, ap) < 0);
80 #endif
81     if (OVF) {
82         Internal ("String size overflow");
83     }
84
85     /* Get the length of the line */
86     Len = strlen (Buf);
87
88     /* Allocate memory */
89     L = xmalloc (sizeof (Line) + Len);
90
91     /* Partially initialize the struct (the remaining fields are initialized
92      * by the caller).
93      */
94     L->Flags    = 0;
95     L->Size     = 0;
96     L->Len      = Len;
97     memcpy (L->Line, Buf, Len+1);
98
99     /* Return the new line */
100     return L;
101 }
102
103
104
105 Line* NewCodeLine (const char* Format, va_list ap)
106 /* Create a new code line and return it */
107 {
108     /* Create a new line struct */
109     Line* L = NewLine (Format, ap);
110
111     /* Initialize struct fields */
112     L->Index    = LineIndex++;
113
114     /* Insert the line into the list */
115     if (FirstLine == 0) {
116         /* The list is empty */
117         L->Next = L->Prev = 0;
118         FirstLine = LastLine = L;
119     } else {
120         /* There are entries in the list, add the new one at the end */
121         LastLine->Next = L;
122         L->Prev = LastLine;
123         L->Next = 0;
124         LastLine = L;
125     }
126
127     /* Return the new line */
128     return L;
129 }
130
131
132
133 Line* NewCodeLineAfter (Line* LineBefore, const char* Format, va_list ap)
134 /* Create a new line, insert it after L and return it. */
135 {
136     /* Create a new line struct */
137     Line* L = NewLine (Format, ap);
138
139     /* Initialize struct fields. We use the same index for the inserted line
140      * as for its predecessor, since we cannot create new numbers on the
141      * fly and the index is only used to determine sort order.
142      */
143     L->Index    = LineBefore->Index;
144
145     /* Insert the line after its predecessor */
146     L->Next = LineBefore->Next;
147     LineBefore->Next = L;
148     L->Prev = LineBefore;
149     if (L->Next) {
150         L->Next->Prev = L;
151     } else {
152         /* This is the last line */
153         LastLine = L;
154     }
155
156     /* Return the new line */
157     return L;
158 }
159
160
161
162 void FreeCodeLine (Line* L)
163 /* Remove a line from the list and free it */
164 {
165     /* Unlink the line */
166     if (L->Prev == 0) {
167         /* No line before this one */
168         FirstLine = L->Next;
169     } else {
170         L->Prev->Next = L->Next;
171     }
172     if (L->Next == 0) {
173         /* No line after this one */
174         LastLine = L->Prev;
175     } else {
176         L->Next->Prev = L->Prev;
177     }
178
179     /* Free the struct */
180     xfree (L);
181 }
182
183
184