1 /*
2  * expand - convert tabs to spaces
3  *
4  * Gunnar Ritter, Freiburg i. Br., Germany, May 2003.
5  */
6 /*
7  * Copyright (c) 2003 Gunnar Ritter
8  *
9  * This software is provided 'as-is', without any express or implied
10  * warranty. In no event will the authors be held liable for any damages
11  * arising from the use of this software.
12  *
13  * Permission is granted to anyone to use this software for any purpose,
14  * including commercial applications, and to alter it and redistribute
15  * it freely, subject to the following restrictions:
16  *
17  * 1. The origin of this software must not be misrepresented; you must not
18  *    claim that you wrote the original software. If you use this software
19  *    in a product, an acknowledgment in the product documentation would be
20  *    appreciated but is not required.
21  *
22  * 2. Altered source versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.
24  *
25  * 3. This notice may not be removed or altered from any source distribution.
26  */
27 
28 /*	Sccsid @(#)tablist.c	1.4 (gritter) 7/16/04	*/
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 
34 #include "atoll.h"
35 
36 #include "tablist.h"
37 
38 struct tab	*tablist;
39 int		tabstop = 8;
40 
41 void
settab(const char * s)42 settab(const char *s)
43 {
44 	char	*x, *y;
45 	long long	n;
46 
47 	if (s == NULL || *s == '\0')
48 		badts();
49 	tablist = 0;
50 	n = strtoll(s, &x, 10);
51 	if (n == 0)
52 		badts();
53 	if (*x == '\0' && *s != '+' && *s != '-') {
54 		tabstop = n;
55 		return;
56 	}
57 	tabstop = 1;
58 	addtab(n);
59 	do {
60 		if (*x == ',' || *x == ' ' || *x == '\t')
61 			x++;
62 		n = strtoll(x, &y, 10);
63 		if (*x == '+' || *x == '-' || x == y)
64 			badts();
65 		x = y;
66 		addtab(n);
67 	} while (*x);
68 }
69 
70 void
addtab(long long n)71 addtab(long long n)
72 {
73 	struct tab	*tp, *tq;
74 
75 	tp = scalloc(1, sizeof *tp);
76 	tp->t_pos = n;
77 	if (tablist) {
78 		for (tq = tablist; tq->t_nxt; tq = tq->t_nxt);
79 		if (tq->t_pos >= tp->t_pos)
80 			badts();
81 		tq->t_nxt = tp;
82 	} else
83 		tablist = tp;
84 }
85 
86 void
badts(void)87 badts(void)
88 {
89 	fprintf(stderr, "Bad tab stop spec\n");
90 	exit(2);
91 }
92 
93 void *
scalloc(size_t nmemb,size_t size)94 scalloc(size_t nmemb, size_t size)
95 {
96 	void	*vp;
97 
98 	if ((vp = calloc(nmemb, size)) == NULL) {
99 		write(2, "no memory\n", 10);
100 		_exit(077);
101 	}
102 	return vp;
103 }
104