a56: Add missing prototypes for alloc() and fixstring()
[linux-libre-firmware.git] / a56 / subs.c
1 /*******************************************************
2  *
3  *  a56 - a DSP56001 assembler
4  *
5  *  Written by Quinn C. Jensen
6  *  July 1990
7  *
8  *******************************************************\
9
10 /*
11  * Copyright (C) 2008 Robert Millan <rmh@aybabtu.com>
12  *
13  * This file is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published
15  * by the Free Software Foundation, either version 3 of the License,
16  * or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see
25  * <http://www.gnu.org/licenses/>.
26  *
27  * This file incorporates work covered by the following copyright and
28  * permission notice:
29  *
30  * Copyright (C) 1990-1994 Quinn C. Jensen
31  *
32  * Permission to use, copy, modify, distribute, and sell this software
33  * and its documentation for any purpose is hereby granted without fee,
34  * provided that the above copyright notice appear in all copies and
35  * that both that copyright notice and this permission notice appear
36  * in supporting documentation.  The author makes no representations
37  * about the suitability of this software for any purpose.  It is
38  * provided "as is" without express or implied warranty.
39  *
40  */
41 static char *Copyright = "Copyright (C) 1990-1994 Quinn C. Jensen";
42
43 /*
44  *  subs.c - Some subroutines for the assembler.
45  *
46  */
47
48 #include "a56.h"
49
50 #define MAX 1024
51
52 FILE *open_read(file)
53 char *file;
54 {
55         FILE *fp;
56
57         if(strcmp(file, "-") == 0)
58                 fp = stdin;
59         else if ((fp = fopen(file, "r")) == NULL) {
60                 perror(file);
61                 exit(1);
62         }       
63         return fp;
64 }
65
66 FILE *open_write(file)
67 char *file;
68 {
69         FILE *fp;
70         if ((fp = fopen(file, "w")) == NULL) {
71                 perror(file);
72                 exit(1);
73         }       
74         return fp;
75 }
76
77 FILE *open_append(file)
78 char *file;
79 {
80         FILE *fp;
81         if ((fp = fopen(file, "a")) == NULL) {
82                 perror(file);
83                 exit(1);
84         }       
85         return fp;
86 }
87
88 fatal(c, a1, a2, a3, a4, a5, a6, a7, a8)
89 char *c, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
90 {
91         fprintf(stderr, c, a1, a2, a3, a4, a5, a6, a7, a8);
92         exit(1);
93 }
94
95 #define TABS 8
96 #define MAX_BUF 256
97
98 char tabbuf[MAX_BUF], *untabn();
99 char *untab(s)  /* expand tabs in s */
100 register char *s;
101 {
102         return untabn(s, TABS);
103 }
104
105 char *untabn(s, stops)  /* expand tabs in s */
106 register char *s;
107 register int stops;
108 {
109         char *o = s;
110
111         /* copy input string into buffer to scan while input string is modified */
112
113         register char *b = tabbuf;
114         
115         strncpy(b, s, MAX_BUF);
116
117         /* iterate until the copy of the input string is depleted */
118
119         while(*b) {
120                 if(*b == '\t') {
121                         do
122                                 *s = ' ';
123                         while ((++s - o) % stops);
124                 } else {
125                         *s = *b; s++;
126                 }
127                 b++;
128         }
129
130         /* null terminate the resultant string */
131
132         *s = '\0';
133
134         return o;
135 }
136
137 char *alloc(size)
138 int size;
139 {
140         char *p = (char *)malloc(size);
141         if(NOT p)
142                 fatal("alloc:  insufficient virtual memory to allocate %d bytes\n", 
143                         size);
144         return p;
145 }
146
147 #define ascii2n(c)  \
148         ((c) >= 'a' ? (c) - 'a' + 10 : ((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0'))
149
150 #define valid(c) ((c) >= '0' && (c) <= '9' || \
151         (c) >= 'A' && (c) <= 'Z' || \
152         (c) >= 'a' && (c) <= 'z')
153
154 strtol(s, p, base)
155 register char *s, **p;
156 register int base;
157 {
158         register long result = 0;
159         register int sign = 0;
160
161         while(*s == ' ' || *s == '\t')
162                 s++;
163
164         if(*s == '-') {
165                 s++;
166                 sign++;
167         }
168
169         while(valid(*s)) {
170                 register int dig = ascii2n(*s);
171                 if(dig >= base)
172                         break;
173                 result *= base;
174                 result += dig;
175                 s++;
176         }
177
178         if(p)
179                 *p = s;
180
181         return sign ? -result : result;
182 }