]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/global/lib/tab.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / contrib / global / lib / tab.c
1 /*
2  * Copyright (c) 1996, 1997, 1998 Shigio Yamaguchi. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed by Shigio Yamaguchi.
15  * 4. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      tab.c                                   8-Oct-98
32  *
33  */
34 #include <stdio.h>
35
36 #include "tab.h"
37
38 static  int     tabs = 8;
39
40 #define TABPOS(i)       ((i)%tabs == 0)
41 /*
42  * settabs: set default tab stop
43  *
44  *      i)      n       tab stop
45  */
46 void
47 settabs(n)
48 int     n;
49 {
50         if (n < 1 || n > 32)
51                 return;
52         tabs = n;
53 }
54 /*
55  * detab: convert tabs into spaces and print
56  *
57  *      i)      op      FILE *
58  *      i)      buf     string including tabs
59  */
60 void
61 detab(op, buf)
62 FILE    *op;
63 char    *buf;
64 {
65         int     src, dst;
66         char    c;
67
68         src = dst = 0;
69         while ((c = buf[src++]) != 0) {
70                 if (c == '\t') {
71                         do {
72                                 (void)putc(' ', op);
73                                 dst++;
74                         } while (!TABPOS(dst));
75                 } else {
76                         (void)putc(c, op);
77                         dst++;
78                 }
79         }
80         (void)putc('\n', op);
81 }
82 /*
83  * entab: convert spaces into tabs
84  *
85  *      io)     buf     string buffer
86  */
87 void
88 entab(buf)
89 char    *buf;
90 {
91         int     blanks = 0;
92         int     pos, src, dst;
93         char    c;
94
95         pos = src = dst = 0;
96         while ((c = buf[src++]) != 0) {
97                 if (c == ' ') {
98                         if (!TABPOS(++pos)) {
99                                 blanks++;               /* count blanks */
100                                 continue;
101                         }
102                         /* don't convert single blank into tab */
103                         buf[dst++] = (blanks == 0) ? ' ' : '\t';
104                 } else if (c == '\t') {
105                         while (!TABPOS(++pos))
106                                 ;
107                         buf[dst++] = '\t';
108                 } else {
109                         ++pos;
110                         while (blanks--)
111                                 buf[dst++] = ' ';
112                         buf[dst++] = c;
113                 }
114                 blanks = 0;
115         }
116         if (blanks > 0)
117                 while (blanks--)
118                         buf[dst++] = ' ';
119         buf[dst] = 0;
120 }