handle with variables
[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.5 2017/12/02 04:32:43 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 "utils.h"
21 #include "vs.h"
22 #include "charmap.h"
23 #include "w.h"
24
25 static int dotag(BW *bw, unsigned char *s, void *obj, int *notify)
26 {
27         unsigned char buf[512];
28         FILE *f;
29         unsigned char *t = NULL;
30
31         if (notify) {
32                 *notify = 1;
33         }
34         if (bw->b->name) {
35                 t = vsncpy(t, 0, sz(bw->b->name));
36                 t = vsncpy(sv(t), sc(":"));
37                 t = vsncpy(sv(t), sv(s));
38         }
39         f = fopen("tags", "r");
40         if (!f) {
41                 msgnw(bw->parent, US "Couldn't open tags file");
42                 vsrm(s);
43                 vsrm(t);
44                 return -1;
45         }
46         while (fgets((char *)buf, 512, f)) {
47                 int x, y, c;
48
49                 for (x = 0; buf[x] && buf[x] != ' ' && buf[x] != '\t'; ++x) ;
50                 c = buf[x];
51                 buf[x] = 0;
52                 if (!strcmp(s, buf) || (t && !strcmp(t, buf))) {
53                         buf[x] = c;
54                         while (buf[x] == ' ' || buf[x] == '\t') {
55                                 ++x;
56                         }
57                         for (y = x; buf[y] && buf[y] != ' ' && buf[y] != '\t' && buf[y] != '\n'; ++y) ;
58                         if (x != y) {
59                                 c = buf[y];
60                                 buf[y] = 0;
61                                 if (doswitch(bw, vsncpy(NULL, 0, sz(buf + x)), NULL, NULL)) {
62                                         vsrm(s);
63                                         vsrm(t);
64                                         fclose(f);
65                                         return -1;
66                                 }
67                                 bw = (BW *) maint->curwin->object;
68                                 buf[y] = c;
69                                 while (buf[y] == ' ' || buf[y] == '\t') {
70                                         ++y;
71                                 }
72                                 for (x = y; buf[x] && buf[x] != '\n'; ++x) ;
73                                 buf[x] = 0;
74                                 if (x != y) {
75                                         long line = 0;
76
77                                         if (buf[y] >= '0' && buf[y] <= '9') {
78                                                 sscanf((char *)(buf + y), "%ld", &line);
79                                                 if (line >= 1) {
80                                                         int omid = mid;
81
82                                                         mid = 1;
83                                                         pline(bw->cursor, line - 1), bw->cursor->xcol = piscol(bw->cursor);
84                                                         dofollows();
85                                                         mid = omid;
86                                                 } else {
87                                                         msgnw(bw->parent, US "Invalid line number");
88                                                 }
89                                         } else {
90                                                 if (buf[y] == '/' || buf[y] == '?') {
91                                                         ++y;
92                                                         if (buf[y] == '^')
93                                                                 buf[--y] = '\\';
94                                                         for (x = y+1; buf[x] && buf[x] != '\n' && buf[x-1] != '/'; ++x);
95                                                 }
96                                                 if (buf[x - 1] == '/' || buf[x - 1] == '?') {
97                                                         --x;
98                                                         buf[x] = 0;
99                                                         if (buf[x - 1] == '$') {
100                                                                 buf[x - 1] = '\\';
101                                                                 buf[x] = '$';
102                                                                 ++x;
103                                                                 buf[x] = 0;
104                                                         }
105                                                 }
106                                                 if (x != y) {
107                                                         vsrm(s);
108                                                         vsrm(t);
109                                                         fclose(f);
110                                                         return dopfnext(bw, mksrch(vsncpy(NULL, 0, sz(buf + y)), NULL, 0, 0, -1, 0, 0), NULL);
111                                                 }
112                                         }
113                                 }
114                                 vsrm(s);
115                                 vsrm(t);
116                                 fclose(f);
117                                 return 0;
118                         }
119                 }
120         }
121         msgnw(bw->parent, US "Not found");
122         vsrm(s);
123         vsrm(t);
124         fclose(f);
125         return -1;
126 }
127
128 static B *taghist = NULL;
129
130 int utag(BW *bw)
131 {
132         BW *pbw;
133
134         pbw = wmkpw(bw->parent, US "Tag search: ", &taghist, dotag, NULL, NULL, cmplt, NULL, NULL, locale_map);
135         if (pbw && joe_isalnum_(bw->b->o.charmap,brch(bw->cursor))) {
136                 P *p = pdup(bw->cursor);
137                 P *q = pdup(p);
138                 int c;
139
140                 while (joe_isalnum_(bw->b->o.charmap,(c = prgetc(p))))
141                         /* do nothing */;
142                 if (c != NO_MORE_DATA) {
143                         pgetc(p);
144                 }
145                 pset(q, p);
146                 while (joe_isalnum_(bw->b->o.charmap,(c = pgetc(q))))
147                         /* do nothing */;
148                 if (c != NO_MORE_DATA) {
149                         prgetc(q);
150                 }
151                 binsb(pbw->cursor, bcpy(p, q));
152                 pset(pbw->cursor, pbw->b->eof);
153                 pbw->cursor->xcol = piscol(pbw->cursor);
154                 prm(p);
155                 prm(q);
156         }
157         if (pbw) {
158                 return 0;
159         } else {
160                 return -1;
161         }
162 }