File: tab.c

package info (click to toggle)
joe 2.8-10
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,080 kB
  • ctags: 1,764
  • sloc: ansic: 18,800; asm: 224; makefile: 122; sh: 9
file content (328 lines) | stat: -rw-r--r-- 6,871 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* File selection menu
   Copyright (C) 1992 Joseph H. Allen

This file is part of JOE (Joe's Own Editor)

JOE is free software; you can redistribute it and/or modify it under the 
terms of the GNU General Public License as published by the Free Software 
Foundation; either version 1, or (at your option) any later version.  

JOE is distributed in the hope that it will be useful, but WITHOUT ANY 
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
details.  

You should have received a copy of the GNU General Public License along with 
JOE; see the file COPYING.  If not, write to the Free Software Foundation, 
675 Mass Ave, Cambridge, MA 02139, USA.  */ 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "scrn.h"
#include "kbd.h"
#include "vs.h"
#include "w.h"
#include "bw.h"
#include "zstr.h"
#include "path.h"
#include "va.h"
#include "menu.h"
#include "tty.h"
#include "blocks.h"
#include "tab.h"

typedef struct tab TAB;

extern int smode;

struct tab
 {
 char *path;	/* Current directory */
 char *pattern;	/* Search pattern */
 int len;	/* No. entries in files */
 char **files;	/* Array of file names */
 char **list;
 char *type;	/* File type array */
 int prv;
 char *orgpath;
 char *orgnam;
 };

/* Type codes for file type array */

#define F_DIR		1
#define F_NORMAL	2
#define F_EXEC		4

/* Read matching files from a directory
 *  Directory is given in tab.path
 *  Pattern is given in tab.pattern
 *
 * Returns with -1 if there was an error
 * Otherwise returns index to file with inode given in prv
 * len and files are set with the file names
 * type is set with the file types
 */

static int get_entries(tab,prv)
TAB *tab;
 {
 int a;
 int which=0;
 char *oldpwd=pwd();
 char **files;
 if(chpwd(tab->path)) return -1;
 files=(char **)rexpnd(tab->pattern);
 if(!files)
  {
  chpwd(oldpwd);
  return -1;
  }
 if(!aLEN(files))
  {
  chpwd(oldpwd);
  return -1;
  }
 tab->len=aLEN(files);
 varm(tab->files); tab->files=files;
 vasort(files,tab->len);
 if(tab->type) free(tab->type);
 tab->type=(char *)malloc(tab->len);
 for(a=0;a!=tab->len;a++)
  {
  struct stat buf;
  mset(&buf,0,sizeof(struct stat));
  stat(files[a],&buf);
  if(buf.st_ino==prv) which=a;
  if((buf.st_mode&S_IFMT)==S_IFDIR) tab->type[a]=F_DIR;
  else if(buf.st_mode&(0100|0010|0001)) tab->type[a]=F_EXEC;
  else tab->type[a]=F_NORMAL;
  }
 chpwd(oldpwd);
 return which;
 }

void insnam(bw,path,nam)
BW *bw;
char *path, *nam;
 {
 P *p=pdup(bw->cursor); pbol(p);
 peol(bw->cursor);
 bdel(p,bw->cursor);
 if(sLEN(path))
  {
  binsm(bw->cursor,sv(path)), peol(bw->cursor);
  if(path[sLEN(path)-1]!='/')
   binsm(bw->cursor,sc("/")), peol(bw->cursor);
  }
 binsm(bw->cursor,sv(nam)); peol(bw->cursor);
 prm(p);
 bw->cursor->xcol=piscol(bw->cursor);
 }

/* Given a menu structure with a tab structure as its object,
 * a pattern and path set in the tab structure:
 *
 * Load the menu with a list of file names and set the file name in
 * the prompt window to the directory the menu was read in from.
 * If flg is set, treload attempts to position to the previous directory
 * which was visited.
 *
 * Returns with -1 if there was an error
 * Returns with 0 for success
 */

int treload(m,flg)
MENU *m;
 {
 TAB *tab=(TAB *)m->object;	/* The menu */
 W *w=m->parent;		/* Window menu is in */
 BW *bw=(BW *)w->win->object;	/* The prompt window */
 int x;
 int which;
 struct stat buf;

 if((which=get_entries(tab,tab->prv))<0) return -1;
 if(tab->path && tab->path[0]) stat(tab->path,&buf);
 else stat(".",&buf);
 tab->prv=buf.st_ino;
 if(!flg) which=0;

 tab->list=vatrunc(tab->list,aLEN(tab->files));

 for(x=0;tab->files[x];++x)
  {
  char *s=vsncpy(NULL,0,sv(tab->files[x]));
  tab->list=vaset(tab->list,x,s);
  if(tab->type[x]==F_DIR)
   tab->list[x]=vsadd(tab->list[x],'/');
  else if(tab->type[x]==F_EXEC)
   tab->list[x]=vsadd(tab->list[x],'*');
  }
 ldmenu(m,tab->list,which);
 insnam(bw,tab->path,tab->pattern);
 return 0;
 }
 
void rmtab(tab)
TAB *tab;
 {
 vsrm(tab->orgpath);
 vsrm(tab->orgnam);
 varm(tab->list);
 vsrm(tab->path);
 vsrm(tab->pattern);
 varm(tab->files);
 if(tab->type) free(tab->type);
 free(tab);
 }

/* The user hit return */

int tabrtn(m,cursor,tab)
MENU *m;
TAB *tab;
 {
 if(tab->type[cursor]==F_DIR)
  { /* Switch directories */
  char *orgpath=tab->path;
  char *orgpattern=tab->pattern;
  char *e=endprt(tab->path);
  if(!zcmp(tab->files[cursor],"..") && sLEN(e) &&
     !(e[0]=='.' && e[1]=='.' && (!e[2] || e[2]=='/')))
   tab->path=begprt(tab->path);
  else
   {
   tab->path=vsncpy(NULL,0,sv(tab->path));
   tab->path=vsncpy(sv(tab->path),sv(m->list[cursor]));
   }
  vsrm(e);
  tab->pattern=vsncpy(NULL,0,sc("*"));
  if(treload(m,0))
   {
   msgnw(m,"Couldn't read directory ");
   vsrm(tab->pattern); tab->pattern=orgpattern;
   vsrm(tab->path); tab->path=orgpath;
   return -1;
   }
  else
   {
   vsrm(orgpattern);
   vsrm(orgpath);
   return 0;
   }
  }
 else
  { /* Select name */
  BW *bw=m->parent->win->object;
  insnam(bw,tab->path,tab->files[cursor]);
  rmtab(tab);
  m->object=0; m->abrt=0;
  wabort(m->parent);
  return 0;
  }
 }

/* The user hit backspace */

int tabbacks(m,cursor,tab)
MENU *m;
TAB *tab;
 {
 char *orgpath=tab->path;
 char *orgpattern=tab->pattern;
 char *e=endprt(tab->path);
 if(sLEN(e)) tab->path=begprt(tab->path);
 else
  {
  wabort(m->parent);
  return 0;
  }
 vsrm(e);
 tab->pattern=vsncpy(NULL,0,sc("*"));

 if(treload(m,1))
  {
  msgnw(m,"Couldn't read directory ");
  vsrm(tab->pattern); tab->pattern=orgpattern;
  vsrm(tab->path); tab->path=orgpath;
  return -1;
  }
 else
  {
  vsrm(orgpattern);
  vsrm(orgpath);
  return 0;
  }
 }

int tababrt(bw,cursor,tab)
BW *bw;
TAB *tab;
 {
 insnam(bw,tab->orgpath,tab->orgnam);
 rmtab(tab);
 return -1;
 }

/* Create a tab window */

int cmplt(bw)
BW *bw;
 {
 MENU *new;
 TAB *tab;
 P *p, *q;
 char *cline, *tmp;
 long a,b;

 tab=(TAB *)malloc(sizeof(TAB));
 new=mkmenu(bw,NULL,tabrtn,tababrt,tabbacks,0,tab,NULL);
 if(!new)
  {
  free(tab);
  return -1;
  }

 tab->files=0;
 tab->type=0;
 tab->list=0;
 tab->prv=0;
 tab->len=0;

 p=pdup(bw->cursor); pbol(p);
 q=pdup(bw->cursor); peol(q);
 tmp=brvs(p,(int)(q->byte-p->byte));
 cline=parsens(tmp,&a,&b);
 vsrm(tmp);
 prm(p); prm(q);

 tab->pattern=namprt(cline);
 tab->path=dirprt(cline);
 tab->orgnam=vsncpy(NULL,0,sv(tab->pattern));
 tab->orgpath=vsncpy(NULL,0,sv(tab->path));
 tab->pattern=vsadd(tab->pattern,'*');
 vsrm(cline);

 if(treload(new,0))
  {
  wabort(new->parent);
  ttputc(7);
  return -1;
  }
 else
  if(sLEN(tab->files)==1) return tabrtn(new,0,tab);
  else if(smode || isreg(tab->orgnam)) return 0;
  else
   {
   char *com=mcomplete(new);
   vsrm(tab->orgnam);
   tab->orgnam=com;
   wabort(new->parent);
   smode=2;
   ttputc(7);
   return 0;
   }
 }