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