new jupp, again fixes
[alioth/jupp.git] / termidx.c
1 /* $MirOS: contrib/code/jupp/termidx.c,v 1.4 2012/06/08 17:05:12 tg Exp $ */
2 /*
3  *      Program to generate termcap index file
4  *      Copyright
5  *              (C) 1992 Joseph H. Allen
6  *
7  * This file is part of JOE (Joe's Own Editor)
8  */
9 #include "config.h"
10 #include "types.h"
11
12 #include <stdio.h>
13 #include <string.h>
14
15 static void gen(unsigned char *s, FILE *fd)
16 {
17         int c, x;
18         off_t addr = 0, oaddr;
19
20       loop:
21         while (c = getc(fd), c == ' ' || c == '\t' || c == '#')
22                 do {
23                         c = getc(fd);
24                 } while (!(c == -1 || c == '\n'));
25         if (c == -1)
26                 return;
27         if (c == '\n')
28                 goto loop;
29         oaddr = addr;
30         addr = ftello(fd) - 1;
31         ungetc(c, fd);
32         s[x = 0] = 0;
33         while (1) {
34                 c = getc(fd);
35                 if (c == -1 || c == '\n') {
36                         if (x != 0 && s[x - 1] == '\\')
37                                 --x;
38                         if (x) {
39                                 int y, z, flg;
40
41                                 s[x] = 0;
42                                 z = 0;
43                                 flg = 0;
44                                 do {
45                                         for (y = z; s[y] && s[y] != '|' && s[y] != ':'; ++y) ;
46                                         c = s[y];
47                                         s[y] = 0;
48                                         if (strlen((char *)(s + z)) > 2 && !strchr((char *)(s + z), ' ') && !strchr((char *)(s + z), '\t')) {
49                                                 if(flg)
50                                                         putchar(' ');
51                                                 fputs((char *)(s + z), stdout);
52                                                 flg = 1;
53                                         }
54                                         s[y] = c;
55                                         z = y + 1;
56                                 } while (c && c != ':');
57                                 if (flg)
58                                         printf(" %lX\n",
59                                             (unsigned long)(addr - oaddr));
60                         }
61                         goto loop;
62                 } else if (c == '\r')
63                         /* do nothing */ ;
64                 else
65                         s[x++] = c;
66         }
67 }
68
69 int main(int argc, char *argv[])
70 {
71         unsigned char array[65536];
72
73         gen(array, stdin);
74         return(0);
75 }