]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/write_entry.c
ncurses 4.1
[ncurses.git] / ncurses / write_entry.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23
24 /*
25  *      write_entry.c -- write a terminfo structure onto the file system
26  */
27
28 #include <curses.priv.h>
29
30 #include <sys/stat.h>
31
32 #include <tic.h>
33 #include <term.h>
34 #include <term_entry.h>
35
36 #ifndef S_ISDIR
37 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
38 #endif
39
40 MODULE_ID("$Id: write_entry.c,v 1.16 1997/05/10 17:33:12 tom Exp $")
41
42 static int total_written;
43
44 static int write_object(FILE *, TERMTYPE *);
45
46 /*
47  *      make_directory(char *path)
48  *
49  *      Make a directory if it doesn't exist.
50  */
51 static int make_directory(const char *path)
52 {
53 int     rc;
54 struct  stat    statbuf;
55 char    fullpath[PATH_MAX];
56 const char *destination = _nc_tic_dir(0);
57
58         if (path == destination || *path == '/')
59                 (void)strcpy(fullpath, path);
60         else
61                 (void)sprintf(fullpath, "%s/%s", destination, path);
62
63         if ((rc = stat(path, &statbuf)) < 0) {
64                 rc = mkdir(path, 0777);
65         } else {
66                 if (access(path, R_OK|W_OK|X_OK) < 0) {
67                         _nc_err_abort("%s: permission denied", fullpath);
68                 } else if (!(S_ISDIR(statbuf.st_mode))) {
69                         _nc_err_abort("%s: not a directory", fullpath);
70                 }
71         }
72         return rc;
73 }
74
75 void  _nc_set_writedir(char *dir)
76 /* set the write directory for compiled entries */
77 {
78     const char *destination;
79
80     if (dir != 0)
81         (void) _nc_tic_dir(dir);
82     else if (getenv("TERMINFO") != NULL)
83         (void) _nc_tic_dir(getenv("TERMINFO"));
84
85     destination = _nc_tic_dir(0);
86     if (make_directory(destination) < 0)
87     {
88         char    *home;
89
90         /* ncurses extension...fall back on user's private directory */
91         if ((home = getenv("HOME")) != (char *)NULL)
92         {
93             char *temp = malloc(sizeof(PRIVATE_INFO) + strlen(home));
94             (void) sprintf(temp, PRIVATE_INFO, home);
95             destination = temp;
96
97             if (make_directory(destination) < 0)
98                 _nc_err_abort("%s: permission denied (errno %d)",
99                         destination, errno);
100         }
101     }
102
103     /*
104      * Note: because of this code, this logic should be exercised
105      * *once only* per run.
106      */
107     if (chdir(_nc_tic_dir(destination)) < 0)
108         _nc_err_abort("%s: not a directory", destination);
109 }
110
111 /*
112  *      check_writeable(char code)
113  *
114  *      Miscellaneous initialisations
115  *
116  *      Check for access rights to destination directories
117  *      Create any directories which don't exist.
118  *
119  */
120
121 static void check_writeable(int code)
122 {
123 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
124 static bool verified[sizeof(dirnames)];
125
126 char            dir[2];
127 char            *s;
128
129         if (code == 0 || (s = strchr(dirnames, code)) == 0)
130             _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code);
131
132         if (verified[s-dirnames])
133             return;
134
135         dir[0] = code;
136         dir[1] = '\0';
137         (void) make_directory(dir);
138
139         verified[s-dirnames] = TRUE;
140 }
141
142 /*
143  *      _nc_write_entry()
144  *
145  *      Save the compiled version of a description in the filesystem.
146  *
147  *      make a copy of the name-list
148  *      break it up into first-name and all-but-last-name
149  *      creat(first-name)
150  *      write object information to first-name
151  *      close(first-name)
152  *      for each name in all-but-last-name
153  *          link to first-name
154  *
155  *      Using 'time()' to obtain a reference for file timestamps is unreliable,
156  *      e.g., with NFS, because the filesystem may have a different time
157  *      reference.  We check for pre-existence of links by latching the first
158  *      timestamp from a file that we create.
159  *
160  *      The _nc_warning() calls will report a correct line number only if
161  *      _nc_curr_line is properly set before the write_entry() call.
162  */
163
164 void _nc_write_entry(TERMTYPE *const tp)
165 {
166 struct stat     statbuf;
167 FILE            *fp;
168 char            name_list[MAX_TERMINFO_LENGTH];
169 char            *first_name, *other_names;
170 char            *ptr;
171 char            filename[PATH_MAX];
172 char            linkname[PATH_MAX];
173 #if USE_SYMLINKS
174 char            symlinkname[PATH_MAX];
175 #endif /* USE_SYMLINKS */
176 static int      call_count;
177 static time_t   start_time;             /* time at start of writes */
178
179         if (call_count++ == 0) {
180                 start_time = 0;
181         }
182
183         (void) strcpy(name_list, tp->term_names);
184         DEBUG(7, ("Name list = '%s'", name_list));
185
186         first_name = name_list;
187
188         ptr = &name_list[strlen(name_list) - 1];
189         other_names = ptr + 1;
190
191         while (ptr > name_list  &&  *ptr != '|')
192                 ptr--;
193
194         if (ptr != name_list) {
195                 *ptr = '\0';
196
197                 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
198                         continue;
199
200                 if (*ptr == '\0')
201                         other_names = ptr;
202                 else {
203                         *ptr = '\0';
204                         other_names = ptr + 1;
205                 }
206         }
207
208         DEBUG(7, ("First name = '%s'", first_name));
209         DEBUG(7, ("Other names = '%s'", other_names));
210
211         _nc_set_type(first_name);
212
213         if (strlen(first_name) > sizeof(filename)-3)
214                 _nc_warning("terminal name too long.");
215
216         sprintf(filename, "%c/%s", first_name[0], first_name);
217
218         /*
219          * Has this primary name been written since the first call to
220          * write_entry()?  If so, the newer write will step on the older,
221          * so warn the user.
222          */
223         if (start_time > 0 &&
224             stat(filename, &statbuf) >= 0
225             && statbuf.st_mtime >= start_time)
226         {
227                 _nc_warning("name multiply defined.");
228         }
229
230         check_writeable(first_name[0]);
231         fp = fopen(filename, "w");
232         if (fp == NULL) {
233                 perror(filename);
234                 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
235         }
236         DEBUG(1, ("Created %s", filename));
237
238         if (write_object(fp, tp) == ERR) {
239                 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
240         }
241         fclose(fp);
242
243         if (start_time == 0) {
244                 if (stat(filename, &statbuf) < 0
245                  || (start_time = statbuf.st_mtime) == 0) {
246                         _nc_syserr_abort("error obtaining time from %s/%s",
247                                 _nc_tic_dir(0), filename);
248                 }
249         }
250         while (*other_names != '\0') {
251                 ptr = other_names++;
252                 while (*other_names != '|'  &&  *other_names != '\0')
253                         other_names++;
254
255                 if (*other_names != '\0')
256                         *(other_names++) = '\0';
257
258                 if (strlen(ptr) > sizeof(linkname)-3) {
259                         _nc_warning("terminal alias %s too long.", ptr);
260                         continue;
261                 }
262
263                 check_writeable(ptr[0]);
264                 sprintf(linkname, "%c/%s", ptr[0], ptr);
265
266                 if (strcmp(filename, linkname) == 0) {
267                         _nc_warning("self-synonym ignored");
268                 }
269                 else if (stat(linkname, &statbuf) >= 0  &&
270                                                 statbuf.st_mtime < start_time)
271                 {
272                         _nc_warning("alias %s multiply defined.", ptr);
273                 }
274                 else
275                 {
276 #if USE_SYMLINKS
277                         strcpy(symlinkname, "../");
278                         strcat(symlinkname, filename);
279 #endif /* USE_SYMLINKS */
280                         unlink(linkname);
281 #if USE_SYMLINKS
282                         if (symlink(symlinkname, linkname) < 0)
283 #else
284                         if (link(filename, linkname) < 0)
285 #endif /* USE_SYMLINKS */
286                             _nc_syserr_abort("can't link %s to %s", filename, linkname);
287                         DEBUG(1, ("Linked %s", linkname));
288                 }
289         }
290 }
291
292 #undef LITTLE_ENDIAN    /* BSD/OS defines this as a feature macro */
293 #define HI(x)                   ((x) / 256)
294 #define LO(x)                   ((x) % 256)
295 #define LITTLE_ENDIAN(p, x)     (p)[0] = LO(x), (p)[1] = HI(x)
296
297 static int write_object(FILE *fp, TERMTYPE *tp)
298 {
299 char            *namelist;
300 size_t          namelen, boolmax, nummax, strmax;
301 char            zero = '\0';
302 size_t          i;
303 short           nextfree;
304 short           offsets[STRCOUNT];
305 unsigned char   buf[MAX_ENTRY_SIZE];
306
307         namelist = tp->term_names;
308         namelen = strlen(namelist) + 1;
309
310         boolmax = 0;
311         for (i = 0; i < BOOLWRITE; i++)
312                 if (tp->Booleans[i])
313                         boolmax = i+1;
314
315         nummax = 0;
316         for (i = 0; i < NUMWRITE; i++)
317                 if (tp->Numbers[i] != ABSENT_NUMERIC)
318                         nummax = i+1;
319
320         strmax = 0;
321         for (i = 0; i < STRWRITE; i++)
322                 if (tp->Strings[i] != ABSENT_STRING)
323                         strmax = i+1;
324
325         nextfree = 0;
326         for (i = 0; i < strmax; i++)
327             if (tp->Strings[i] == ABSENT_STRING)
328                 offsets[i] = -1;
329             else if (tp->Strings[i] == CANCELLED_STRING)
330                 offsets[i] = -2;
331             else
332             {
333                 offsets[i] = nextfree;
334                 nextfree += strlen(tp->Strings[i]) + 1;
335             }
336
337         /* fill in the header */
338         LITTLE_ENDIAN(buf,    MAGIC);
339         LITTLE_ENDIAN(buf+2,  min(namelen, MAX_NAME_SIZE + 1));
340         LITTLE_ENDIAN(buf+4,  boolmax);
341         LITTLE_ENDIAN(buf+6,  nummax);
342         LITTLE_ENDIAN(buf+8,  strmax);
343         LITTLE_ENDIAN(buf+10, nextfree);
344
345         /* write out the header */
346         if (fwrite(buf, 12, 1, fp) != 1
347                 ||  fwrite(namelist, sizeof(char), (size_t)namelen, fp) != namelen
348                 ||  fwrite(tp->Booleans, sizeof(char), (size_t)boolmax, fp) != boolmax)
349                 return(ERR);
350
351         /* the even-boundary padding byte */
352         if ((namelen+boolmax) % 2 != 0 &&  fwrite(&zero, sizeof(char), 1, fp) != 1)
353                 return(ERR);
354
355 #ifdef SHOWOFFSET
356         (void) fprintf(stderr, "Numerics begin at %04lx\n", ftell(fp));
357 #endif /* SHOWOFFSET */
358
359         /* the numerics */
360         for (i = 0; i < nummax; i++)
361         {
362                 if (tp->Numbers[i] == -1)       /* HI/LO won't work */
363                         buf[2*i] = buf[2*i + 1] = 0377;
364                 else
365                         LITTLE_ENDIAN(buf + 2*i, tp->Numbers[i]);
366         }
367         if (fwrite(buf, 2, (size_t)nummax, fp) != nummax)
368                 return(ERR);
369
370 #ifdef SHOWOFFSET
371         (void) fprintf(stderr, "String offets begin at %04lx\n", ftell(fp));
372 #endif /* SHOWOFFSET */
373
374         /* the string offsets */
375         for (i = 0; i < strmax; i++)
376                 if (offsets[i] == -1)   /* HI/LO won't work */
377                         buf[2*i] = buf[2*i + 1] = 0377;
378                 else if (offsets[i] == -2)      /* HI/LO won't work */
379                 {
380                         buf[2*i] = 0376;
381                         buf[2*i + 1] = 0377;
382                 }
383                 else
384                         LITTLE_ENDIAN(buf + 2*i, offsets[i]);
385         if (fwrite(buf, 2, (size_t)strmax, fp) != strmax)
386                 return(ERR);
387
388 #ifdef SHOWOFFSET
389         (void) fprintf(stderr, "String table begins at %04lx\n", ftell(fp));
390 #endif /* SHOWOFFSET */
391
392         /* the strings */
393         for (i = 0; i < strmax; i++)
394             if (tp->Strings[i] != ABSENT_STRING && tp->Strings[i] != CANCELLED_STRING)
395                 if (fwrite(tp->Strings[i], sizeof(char), strlen(tp->Strings[i]) + 1, fp) != strlen(tp->Strings[i]) + 1)
396                     return(ERR);
397
398         total_written++;
399         return(OK);
400 }
401
402 /*
403  * Returns the total number of entries written by this process
404  */
405 int _nc_tic_written(void)
406 {
407         return total_written;
408 }