another update from CVS HEAD, for QA
[alioth/jupp.git] / utag.c
1 /*
2  *      tags file symbol lookup
3  *      Copyright
4  *              (C) 1992 Joseph H. Allen
5  *
6  *      This file is part of JOE (Joe's Own Editor)
7  */
8 #include "config.h"
9 #include "types.h"
10
11 __RCSID("$MirOS: contrib/code/jupp/utag.c,v 1.11 2017/12/07 02:10:19 tg Exp $");
12
13 #include "b.h"
14 #include "bw.h"
15 #include "main.h"
16 #include "pw.h"
17 #include "tab.h"
18 #include "ufile.h"
19 #include "usearch.h"
20 #include "utag.h"
21 #include "utils.h"
22 #include "vs.h"
23 #include "charmap.h"
24 #include "w.h"
25
26 static int dotag(BW *bw, unsigned char *s, void *obj, int *notify)
27 {
28         unsigned char buf[512];
29         FILE *f;
30         unsigned char *t = NULL;
31
32         if (notify) {
33                 *notify = 1;
34         }
35         if (bw->b->name) {
36                 t = vsncpy(t, 0, sz(bw->b->name));
37                 t = vsncpy(sv(t), sc(":"));
38                 t = vsncpy(sv(t), sv(s));
39         }
40         f = fopen("tags", "r");
41         if (!f) {
42                 msgnw(bw->parent, UC "Couldn't open tags file");
43                 vsrm(s);
44                 vsrm(t);
45                 return -1;
46         }
47         while (fgets((char *)buf, 512, f)) {
48                 int x, y, c;
49
50                 for (x = 0; buf[x] && buf[x] != ' ' && buf[x] != '\t'; ++x) ;
51                 c = buf[x];
52                 buf[x] = 0;
53                 if (!strcmp(s, buf) || (t && !strcmp(t, buf))) {
54                         buf[x] = c;
55                         while (buf[x] == ' ' || buf[x] == '\t') {
56                                 ++x;
57                         }
58                         for (y = x; buf[y] && buf[y] != ' ' && buf[y] != '\t' && buf[y] != '\n'; ++y) ;
59                         if (x != y) {
60                                 c = buf[y];
61                                 buf[y] = 0;
62                                 if (doswitch(bw, vsncpy(NULL, 0, sz(buf + x)), NULL, NULL)) {
63                                         vsrm(s);
64                                         vsrm(t);
65                                         fclose(f);
66                                         return -1;
67                                 }
68                                 bw = maint->curwin->object.bw;
69                                 buf[y] = c;
70                                 while (buf[y] == ' ' || buf[y] == '\t') {
71                                         ++y;
72                                 }
73                                 for (x = y; buf[x] && buf[x] != '\n'; ++x) ;
74                                 buf[x] = 0;
75                                 if (x != y) {
76                                         long line = 0;
77
78                                         if (buf[y] >= '0' && buf[y] <= '9') {
79                                                 line = ustol(buf + y, NULL, USTOL_AUTO);
80                                                 if (line >= 1) {
81                                                         int omid = mid;
82
83                                                         mid = 1;
84                                                         pline(bw->cursor, line - 1), bw->cursor->xcol = piscol(bw->cursor);
85                                                         dofollows();
86                                                         mid = omid;
87                                                 } else {
88                                                         msgnw(bw->parent, UC "Invalid line number");
89                                                 }
90                                         } else {
91                                                 if (buf[y] == '/' || buf[y] == '?') {
92                                                         ++y;
93                                                         if (buf[y] == '^')
94                                                                 buf[--y] = '\\';
95                                                         for (x = y+1; buf[x] && buf[x] != '\n' && buf[x-1] != '/'; ++x);
96                                                 }
97                                                 if (buf[x - 1] == '/' || buf[x - 1] == '?') {
98                                                         --x;
99                                                         buf[x] = 0;
100                                                         if (buf[x - 1] == '$') {
101                                                                 buf[x - 1] = '\\';
102                                                                 buf[x] = '$';
103                                                                 ++x;
104                                                                 buf[x] = 0;
105                                                         }
106                                                 }
107                                                 if (x != y) {
108                                                         vsrm(s);
109                                                         vsrm(t);
110                                                         fclose(f);
111                                                         return dopfnext(bw, mksrch(vsncpy(NULL, 0, sz(buf + y)), NULL, 0, 0, -1, 0, 0), NULL);
112                                                 }
113                                         }
114                                 }
115                                 vsrm(s);
116                                 vsrm(t);
117                                 fclose(f);
118                                 return 0;
119                         }
120                 }
121         }
122         msgnw(bw->parent, UC "Not found");
123         vsrm(s);
124         vsrm(t);
125         fclose(f);
126         return -1;
127 }
128
129 static B *taghist = NULL;
130
131 int utag(BW *bw)
132 {
133         BW *pbw;
134
135         pbw = wmkpw(bw->parent, UC "Tag search: ", &taghist, dotag, NULL, NULL, cmplt, NULL, NULL, locale_map);
136         if (pbw && joe_isalnux(bw->b->o.charmap,brch(bw->cursor))) {
137                 P *p = pdup(bw->cursor);
138                 P *q = pdup(p);
139                 int c;
140
141                 while (joe_isalnux(bw->b->o.charmap,(c = prgetc(p))))
142                         /* do nothing */;
143                 if (c != NO_MORE_DATA) {
144                         pgetc(p);
145                 }
146                 pset(q, p);
147                 while (joe_isalnux(bw->b->o.charmap,(c = pgetc(q))))
148                         /* do nothing */;
149                 if (c != NO_MORE_DATA) {
150                         prgetc(q);
151                 }
152                 binsb(pbw->cursor, bcpy(p, q));
153                 pset(pbw->cursor, pbw->b->eof);
154                 pbw->cursor->xcol = piscol(pbw->cursor);
155                 prm(p);
156                 prm(q);
157         }
158         if (pbw) {
159                 return 0;
160         } else {
161                 return -1;
162         }
163 }