~ubuntu-branches/debian/sid/x11-apps/sid

« back to all changes in this revision

Viewing changes to xman/misc.c

  • Committer: Package Import Robot
  • Author(s): Julien Cristau
  • Date: 2013-12-25 18:39:25 UTC
  • Revision ID: package-import@ubuntu.com-20131225183925-kh1rc0smebkuyxf7
Tags: 7.7+2
* xclipboard 1.1.3
* xclock 1.0.7
* xman 1.1.3
* Use dh.
* Disable silent build rules.
* Remove Cyril from Uploaders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $XConsortium: misc.c,v 1.31 94/12/16 21:36:53 gildea Exp $ */
2
 
/* $XdotOrg: xc/programs/xman/misc.c,v 1.6 2004/09/02 08:40:33 kem Exp $ */
3
1
/*
4
2
 
5
3
Copyright (c) 1987, 1988  X Consortium
29
27
from the X Consortium.
30
28
 
31
29
*/
32
 
/* $XFree86: xc/programs/xman/misc.c,v 1.10 2003/08/02 17:35:48 herrb Exp $ */
33
30
 
34
31
/*
35
32
 * xman - X window system manual page display program.
43
40
 
44
41
#include "globals.h"
45
42
#include "vendor.h"
46
 
#include <X11/Xos.h>            /* sys/types.h and unistd.h included in here */
 
43
#include <X11/Xos.h>            /* sys/types.h and unistd.h included in here */
47
44
#include <sys/stat.h>
48
45
#include <errno.h>
49
46
#include <X11/Xaw/Dialog.h>
50
47
#include <X11/Shell.h>
51
48
 
52
 
static FILE * Uncompress(ManpageGlobals * man_globals, char * filename);
53
 
#ifndef HAS_MKSTEMP
54
 
static Boolean UncompressNamed(ManpageGlobals * man_globals, char * filename,
55
 
                               char * output);
56
 
static Boolean UncompressUnformatted(ManpageGlobals * man_globals,
57
 
                                     char * entry, char * filename);
58
 
#else
59
 
static Boolean UncompressNamed(ManpageGlobals * man_globals, char * filename,
60
 
                               char * output, FILE ** output_fd);
61
 
static Boolean UncompressUnformatted(ManpageGlobals * man_globals,
62
 
                                     char * entry, char * filename,
63
 
                                     FILE **file);
64
 
#endif
 
49
static FILE *Uncompress(ManpageGlobals * man_globals, const char *filename);
 
50
 
 
51
static Boolean UncompressNamed(ManpageGlobals * man_globals,
 
52
                               const char *filename, char *output,
 
53
                               FILE ** output_file);
 
54
static Boolean UncompressUnformatted(ManpageGlobals * man_globals,
 
55
                                     const char *entry, char *filename,
 
56
                                     FILE ** file);
 
57
 
65
58
#ifdef HANDLE_ROFFSEQ
66
 
static Boolean ConstructCommand(char * cmdbuf, char * path, char * filename, char * tempfile);
 
59
static Boolean ConstructCommand(char *cmdbuf, const char *path,
 
60
                                const char *filename, const char *tempfile);
67
61
#endif
68
62
 
69
63
#if defined(ISC) || defined(__SCO__) || defined(__UNIXWARE__)
70
64
static char *uncompress_format = NULL;
71
 
static char *uncompress_formats[] =
72
 
      {  UNCOMPRESS_FORMAT_1,
73
 
         UNCOMPRESS_FORMAT_2,
74
 
         UNCOMPRESS_FORMAT_3
75
 
      };
 
65
 
 
66
static char *uncompress_formats[] = {
 
67
    UNCOMPRESS_FORMAT_1,
 
68
    UNCOMPRESS_FORMAT_2,
 
69
    UNCOMPRESS_FORMAT_3
 
70
};
76
71
#endif
77
72
 
78
73
/*      Function Name: PopupWarning
79
 
 *      Description: This function pops upa warning message.
 
74
 *      Description: This function pops up a warning message.
80
75
 *      Arguments: string - the specific warning string.
81
76
 *      Returns: none
82
77
 */
86
81
static void
87
82
PopdownWarning(Widget w, XtPointer client, XtPointer call)
88
83
{
89
 
  XtPopdown((Widget)client);
 
84
    XtPopdown((Widget) client);
90
85
}
91
86
 
92
87
void
93
 
PopupWarning(ManpageGlobals * man_globals, const char * string)
 
88
PopupWarning(ManpageGlobals * man_globals, const char *string)
94
89
{
95
 
  int n;
96
 
  Arg wargs[3];
97
 
  Dimension topX, topY;
98
 
  char buffer[BUFSIZ];
99
 
  Boolean hasPosition;
100
 
 
101
 
  snprintf( buffer, sizeof(buffer), "Xman Warning: %s", string);
102
 
  hasPosition = FALSE;
103
 
  if (top)
104
 
  {
105
 
    n=0;
106
 
    XtSetArg(wargs[n], XtNx, &topX); n++;
107
 
    XtSetArg(wargs[n], XtNy, &topY); n++;
108
 
    XtGetValues(top, wargs, n);
109
 
    hasPosition = TRUE;
110
 
  }
111
 
 
112
 
  if (man_globals != NULL)
113
 
    ChangeLabel(man_globals->label, buffer);
114
 
  if (man_globals->label == NULL) {
115
 
    n=0;
116
 
    if (hasPosition)
117
 
    {
118
 
      XtSetArg(wargs[n], XtNx, topX); n++;
119
 
      XtSetArg(wargs[n], XtNy, topY); n++;
120
 
    }
121
 
    XtSetArg(wargs[n], XtNtransientFor, top); n++;
122
 
    warnShell = XtCreatePopupShell("warnShell", transientShellWidgetClass,
123
 
                                   initial_widget, wargs, n);
124
 
    XtSetArg(wargs[0], XtNlabel, buffer);
125
 
    warnDialog = XtCreateManagedWidget("warnDialog", dialogWidgetClass,
126
 
                                       warnShell, wargs, 1);
127
 
    XawDialogAddButton(warnDialog, "dismiss", PopdownWarning,
128
 
                       (XtPointer)warnShell);
129
 
    XtRealizeWidget(warnShell);
130
 
    Popup(warnShell, XtGrabNone);
131
 
  }
 
90
    int n;
 
91
    Arg wargs[3];
 
92
    Dimension topX, topY;
 
93
    char buffer[BUFSIZ];
 
94
    Boolean hasPosition;
 
95
 
 
96
    snprintf(buffer, sizeof(buffer), "Xman Warning: %s", string);
 
97
    hasPosition = FALSE;
 
98
    if (top) {
 
99
        n = 0;
 
100
        XtSetArg(wargs[n], XtNx, &topX);
 
101
        n++;
 
102
        XtSetArg(wargs[n], XtNy, &topY);
 
103
        n++;
 
104
        XtGetValues(top, wargs, n);
 
105
        hasPosition = TRUE;
 
106
    }
 
107
 
 
108
    if (man_globals != NULL)
 
109
        ChangeLabel(man_globals->label, buffer);
 
110
    if (man_globals->label == NULL) {
 
111
        n = 0;
 
112
        if (hasPosition) {
 
113
            XtSetArg(wargs[n], XtNx, topX);
 
114
            n++;
 
115
            XtSetArg(wargs[n], XtNy, topY);
 
116
            n++;
 
117
        }
 
118
        XtSetArg(wargs[n], XtNtransientFor, top);
 
119
        n++;
 
120
        warnShell = XtCreatePopupShell("warnShell", transientShellWidgetClass,
 
121
                                       initial_widget, wargs, n);
 
122
        XtSetArg(wargs[0], XtNlabel, buffer);
 
123
        warnDialog = XtCreateManagedWidget("warnDialog", dialogWidgetClass,
 
124
                                           warnShell, wargs, 1);
 
125
        XawDialogAddButton(warnDialog, "dismiss", PopdownWarning,
 
126
                           (XtPointer) warnShell);
 
127
        XtRealizeWidget(warnShell);
 
128
        Popup(warnShell, XtGrabNone);
 
129
    }
132
130
}
133
131
 
134
132
/*      Function Name: PrintError
135
133
 *      Description: This Function prints an error message and exits.
136
134
 *      Arguments: string - the specific message.
137
 
 *      Returns: none. - exits tho.
 
135
 *      Returns: none. - exits though.
138
136
 */
139
137
 
140
138
void
141
 
PrintError(char * string)
 
139
PrintError(const char *string)
142
140
{
143
 
  fprintf(stderr,"Xman Error: %s\n",string);
144
 
  exit(EXIT_FAILURE);
 
141
    fprintf(stderr, "Xman Error: %s\n", string);
 
142
    exit(EXIT_FAILURE);
145
143
}
146
144
 
147
145
/*      Function Name: OpenFile
148
 
 *      Description: Assignes a file to the manpage.
 
146
 *      Description: Assigns a file to the manpage.
149
147
 *      Arguments: man_globals - global structure.
150
148
 *                 file        - the file pointer.
151
149
 *      Returns: none
154
152
void
155
153
OpenFile(ManpageGlobals * man_globals, FILE * file)
156
154
{
157
 
  Arg arglist[1];
158
 
  Cardinal num_args = 0;
159
 
  
160
 
  if (man_globals->curr_file) {
161
 
#if 0 /* Ownership rules need to be fixed first */
162
 
    fclose(man_globals->curr_file);
 
155
    Arg arglist[1];
 
156
    Cardinal num_args = 0;
 
157
 
 
158
    if (man_globals->curr_file) {
 
159
#if 0                           /* Ownership rules need to be fixed first */
 
160
        fclose(man_globals->curr_file);
163
161
#endif
164
 
  }
165
 
  man_globals->curr_file = file;
 
162
    }
 
163
    man_globals->curr_file = file;
166
164
 
167
 
  XtSetArg(arglist[num_args], XtNfile, man_globals->curr_file); num_args++;
168
 
  XtSetValues(man_globals->manpagewidgets.manpage, arglist, num_args);
 
165
    XtSetArg(arglist[num_args], XtNfile, man_globals->curr_file);
 
166
    num_args++;
 
167
    XtSetValues(man_globals->manpagewidgets.manpage, arglist, num_args);
169
168
}
170
169
 
171
170
 
178
177
 *
179
178
 * NOTES:
180
179
 *
181
 
 * If there is a uncompressed section it will look there for uncompresed
 
180
 * If there is a uncompressed section it will look there for uncompressed
182
181
 * manual pages first and then for individually compressed file in the
183
182
 * uncompressed section.
184
183
 *
191
190
FILE *
192
191
FindManualFile(ManpageGlobals * man_globals, int section_num, int entry_num)
193
192
{
194
 
  FILE * file;
195
 
  char path[BUFSIZ], page[BUFSIZ], section[BUFSIZ], *temp;
196
 
  char filename[BUFSIZ];
197
 
  char * entry = manual[section_num].entries[entry_num];
198
 
  int len_cat = strlen(CAT);
 
193
    FILE *file;
 
194
    char path[BUFSIZ], page[BUFSIZ], section[BUFSIZ], *temp;
 
195
    char filename[BUFSIZ];
 
196
    const char *entry = manual[section_num].entries[entry_num];
 
197
    int len_cat = strlen(CAT);
 
198
 
199
199
#if defined(ISC) || defined(__SCO__) || defined(__UNIXWARE__)
200
 
  int i;
 
200
    int i;
201
201
#endif
202
202
 
203
 
  temp = CreateManpageName(entry, 0, 0);
204
 
  snprintf(man_globals->manpage_title, sizeof(man_globals->manpage_title),
205
 
    "The current manual page is: %s.", temp);
206
 
  XtFree(temp);
 
203
    temp = CreateManpageName(entry, 0, 0);
 
204
    snprintf(man_globals->manpage_title, sizeof(man_globals->manpage_title),
 
205
             "The current manual page is: %s.", temp);
 
206
    XtFree(temp);
207
207
 
208
 
  ParseEntry(entry, path, section, page);
 
208
    ParseEntry(entry, path, section, page);
209
209
 
210
210
/*
211
211
 * Look for uncompressed files first.
212
212
 */
213
213
#if defined(__OpenBSD__) || defined(__NetBSD__)
214
 
  /* look in machine subdir first */
215
 
  snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s", path, CAT,
216
 
          section + len_cat, MACHINE, page);
217
 
  if ( (file = fopen(filename,"r")) != NULL)
218
 
    return(file);
 
214
    /* look in machine subdir first */
 
215
    snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s", path, CAT,
 
216
             section + len_cat, MACHINE, page);
 
217
    if ((file = fopen(filename, "r")) != NULL)
 
218
        return (file);
219
219
#endif
220
220
 
221
 
  snprintf(filename, sizeof(filename), "%s/%s%s/%s", 
222
 
    path, CAT, section + len_cat, page);
223
 
  if ( (file = fopen(filename,"r")) != NULL)
224
 
    return(file);
 
221
    snprintf(filename, sizeof(filename), "%s/%s%s/%s",
 
222
             path, CAT, section + len_cat, page);
 
223
    if ((file = fopen(filename, "r")) != NULL)
 
224
        return (file);
225
225
 
226
226
/*
227
227
 * Then for compressed files in an uncompressed directory.
229
229
 
230
230
#if !defined(ISC) && !defined(__UNIXWARE__)
231
231
#if defined(__OpenBSD__) || defined(__NetBSD__)
232
 
  /* look in machine subdir first */
233
 
  snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s.%s", path, CAT,
234
 
          section + len_cat, MACHINE, page, COMPRESSION_EXTENSION);
235
 
  if ( (file = Uncompress(man_globals, filename)) != NULL)
236
 
    return(file);
 
232
    /* look in machine subdir first */
 
233
    snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s.%s", path, CAT,
 
234
             section + len_cat, MACHINE, page, COMPRESSION_EXTENSION);
 
235
    if ((file = Uncompress(man_globals, filename)) != NULL)
 
236
        return (file);
237
237
#endif
238
 
  snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
239
 
          section + len_cat, page, COMPRESSION_EXTENSION);
240
 
  if ( (file = Uncompress(man_globals, filename)) != NULL)
241
 
    return(file);
 
238
    snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
 
239
             section + len_cat, page, COMPRESSION_EXTENSION);
 
240
    if ((file = Uncompress(man_globals, filename)) != NULL)
 
241
        return (file);
242
242
#ifdef GZIP_EXTENSION
243
 
  else {
 
243
    else {
244
244
#if defined(__OpenBSD__) || defined(__NetBSD__)
245
 
      /* look in machine subdir first */
246
 
      snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s.%s", path, CAT,
247
 
              section + len_cat, MACHINE, page, GZIP_EXTENSION);
248
 
      if ( (file = Uncompress(man_globals, filename)) != NULL)
249
 
          return(file);
 
245
        /* look in machine subdir first */
 
246
        snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s.%s", path, CAT,
 
247
                 section + len_cat, MACHINE, page, GZIP_EXTENSION);
 
248
        if ((file = Uncompress(man_globals, filename)) != NULL)
 
249
            return (file);
250
250
#endif
251
 
    snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
252
 
            section + len_cat, page, GZIP_EXTENSION);
253
 
    if ( (file = Uncompress(man_globals, filename)) != NULL)
254
 
      return(file);
255
 
  }
 
251
        snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
 
252
                 section + len_cat, page, GZIP_EXTENSION);
 
253
        if ((file = Uncompress(man_globals, filename)) != NULL)
 
254
            return (file);
 
255
    }
256
256
#endif
257
257
#ifdef BZIP2_EXTENSION
258
258
#if defined(__OpenBSD__) || defined(__NetBSD__)
259
 
      /* look in machine subdir first */
260
 
      snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s.%s", path, CAT,
261
 
              section + len_cat, MACHINE, page, BZIP2_EXTENSION);
262
 
      if ( (file = Uncompress(man_globals, filename)) != NULL)
263
 
          return(file);
 
259
    /* look in machine subdir first */
 
260
    snprintf(filename, sizeof(filename), "%s/%s%s/%s/%s.%s", path, CAT,
 
261
             section + len_cat, MACHINE, page, BZIP2_EXTENSION);
 
262
    if ((file = Uncompress(man_globals, filename)) != NULL)
 
263
        return (file);
264
264
#endif
265
 
  {
266
 
    snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
267
 
            section + len_cat, page, BZIP2_EXTENSION);
268
 
    if ( (file = Uncompress(man_globals, filename)) != NULL)
269
 
      return(file);
270
 
  }
 
265
    {
 
266
        snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
 
267
                 section + len_cat, page, BZIP2_EXTENSION);
 
268
        if ((file = Uncompress(man_globals, filename)) != NULL)
 
269
            return (file);
 
270
    }
271
271
#endif
272
272
#ifdef LZMA_EXTENSION
273
 
  {
274
 
    snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
275
 
            section + len_cat, page, LZMA_EXTENSION);
276
 
    if ( (file = Uncompress(man_globals, filename)) != NULL)
277
 
      return(file);
278
 
  }
 
273
    {
 
274
        snprintf(filename, sizeof(filename), "%s/%s%s/%s.%s", path, CAT,
 
275
                 section + len_cat, page, LZMA_EXTENSION);
 
276
        if ((file = Uncompress(man_globals, filename)) != NULL)
 
277
            return (file);
 
278
    }
279
279
#endif
280
280
#else
281
 
  for(i = 0; i < strlen(COMPRESSION_EXTENSIONS); i++) {
282
 
      snprintf(filename, sizeof(filename), "%s/%s%s/%s.%c", path, CAT,
283
 
            section + len_cat, page, COMPRESSION_EXTENSIONS[i]);
284
 
      uncompress_format = uncompress_formats[i];
 
281
    for (i = 0; i < strlen(COMPRESSION_EXTENSIONS); i++) {
 
282
        snprintf(filename, sizeof(filename), "%s/%s%s/%s.%c", path, CAT,
 
283
                 section + len_cat, page, COMPRESSION_EXTENSIONS[i]);
 
284
        uncompress_format = uncompress_formats[i];
285
285
#ifdef DEBUG
286
 
      printf("Trying .%c ...\n", COMPRESSION_EXTENSIONS[i]);
 
286
        printf("Trying .%c ...\n", COMPRESSION_EXTENSIONS[i]);
287
287
#endif
288
 
      if ( (file = Uncompress(man_globals, filename)) != NULL)
289
 
        return(file);
290
 
  }
 
288
        if ((file = Uncompress(man_globals, filename)) != NULL)
 
289
            return (file);
 
290
    }
291
291
#endif
292
292
 
293
293
/*
298
298
 * HP does it this way (really :-).
299
299
 */
300
300
 
301
 
  snprintf(filename, sizeof(filename), "%s/%s%s.%s/%s", path, CAT, 
302
 
           section + len_cat, COMPRESSION_EXTENSION, page);
303
 
  if ( (file = Uncompress(man_globals, filename)) != NULL)
304
 
    return(file);
 
301
    snprintf(filename, sizeof(filename), "%s/%s%s.%s/%s", path, CAT,
 
302
             section + len_cat, COMPRESSION_EXTENSION, page);
 
303
    if ((file = Uncompress(man_globals, filename)) != NULL)
 
304
        return (file);
305
305
/*
306
306
 * We did not find any preformatted manual pages, try to format it.
307
307
 */
308
308
 
309
 
  return(Format(man_globals, entry));
310
 
}
 
309
    return (Format(man_globals, entry));
 
310
}
 
311
 
 
312
#ifndef HAVE_MKSTEMP
 
313
/* Emulate mkstemp to allow use of a common API in the many calls below */
 
314
_X_HIDDEN int
 
315
Xmkstemp (char *template)
 
316
{
 
317
    int fd = 0;
 
318
    char tmp[PATH_MAX];
 
319
 
 
320
    if (strlen(template) >= sizeof(tmp))
 
321
        return -1;
 
322
    /* save copy of unmodified template in case we have to try again */
 
323
    strcpy(tmp, template);
 
324
 
 
325
    do {
 
326
        if (fd == -1)
 
327
            strcpy(template, tmp);
 
328
        if ((mktemp(template) == NULL) || (template[0] == '\0'))
 
329
            return -1;
 
330
        fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
 
331
    } while ((fd == -1) && (errno == EEXIST || errno == EINTR));
 
332
 
 
333
    return fd;
 
334
}
 
335
#endif
311
336
 
312
337
/*      Function Namecompress
313
338
 *      Description: This function will attempt to find a compressed man
314
339
 *                   page and uncompress it.
315
 
 *      Arguments: man_globals - the psuedo global info.
 
340
 *      Arguments: man_globals - the pseudo global info.
316
341
 *                 filename - name of file to uncompress.
317
342
 *      Returns:; a pointer to the file or NULL.
318
343
 */
319
344
 
320
345
static FILE *
321
 
Uncompress(ManpageGlobals * man_globals, char * filename)
 
346
Uncompress(ManpageGlobals * man_globals, const char *filename)
322
347
{
323
 
  char tmp_file[BUFSIZ];
324
 
  FILE * file;
325
 
 
326
 
#ifndef HAS_MKSTEMP
327
 
  if ( !UncompressNamed(man_globals, filename, tmp_file) )
328
 
    return(NULL);
329
 
 
330
 
  else if ((file = fopen(tmp_file, "r")) == NULL) {
331
 
      PopupWarning(man_globals, "Something went wrong in retrieving the "
332
 
                   "uncompressed manual page try cleaning up /tmp.");
333
 
  }
334
 
#else
335
 
  if (!UncompressNamed(man_globals, filename, tmp_file, &file)) {
336
 
      PopupWarning(man_globals, "Something went wrong in retrieving the "
337
 
                   "uncompressed manual page try cleaning up /tmp.");
338
 
      return(NULL);
339
 
  }
340
 
#endif
341
 
 
342
 
  unlink(tmp_file);             /* remove name in tree, it will remain
343
 
                                   until we close the fd, however. */
344
 
  return(file);
 
348
    char tmp_file[BUFSIZ];
 
349
    FILE *file;
 
350
 
 
351
    if (!UncompressNamed(man_globals, filename, tmp_file, &file)) {
 
352
        PopupWarning(man_globals, "Something went wrong in retrieving the "
 
353
                     "uncompressed manual page try cleaning up /tmp.");
 
354
        return (NULL);
 
355
    }
 
356
 
 
357
    remove(tmp_file);           /* remove name in tree, it will remain
 
358
                                   until we close the fd, however. */
 
359
    return (file);
345
360
}
346
361
 
347
362
/*      Function Name: UncompressNamed
348
363
 *      Description: This function will attempt to find a compressed man
349
364
 *                   page and uncompress it.
350
 
 *      Arguments: man_globals - the psuedo global info.
 
365
 *      Arguments: man_globals - the pseudo global info.
351
366
 *                 filename - name of file to uncompress.
352
367
 * RETURNED        output - the file name output (must be an allocated string).
353
368
 *      Returns:; TRUE if the file was found.
354
369
 */
355
370
 
356
 
#ifndef HAS_MKSTEMP
357
 
static Boolean
358
 
UncompressNamed(ManpageGlobals * man_globals, char * filename, char * output)
359
 
#else
360
 
static Boolean
361
 
UncompressNamed(ManpageGlobals * man_globals, char * filename, char * output,
362
 
                FILE ** output_fd)
363
 
#endif
 
371
static Boolean
 
372
UncompressNamed(ManpageGlobals * man_globals, const char *filename,
 
373
                char *output, FILE ** output_file)
364
374
{
365
 
  char tmp[BUFSIZ], cmdbuf[BUFSIZ], error_buf[BUFSIZ];
366
 
  struct stat junk;
367
 
#ifdef HAS_MKSTEMP
368
 
  int fd;
369
 
#endif
 
375
    char tmp[BUFSIZ], cmdbuf[BUFSIZ], error_buf[BUFSIZ];
 
376
    struct stat junk;
 
377
    int fd;
370
378
 
371
 
  if (stat(filename, &junk) != 0) { /* Check for existance of the file. */
372
 
    if (errno != ENOENT) {
373
 
      snprintf(error_buf, sizeof(error_buf),
374
 
               "Error while stating file %s, errno = %d", filename, errno);
375
 
      PopupWarning(man_globals, error_buf);
 
379
    if (stat(filename, &junk) != 0) {   /* Check for existence of the file. */
 
380
        if (errno != ENOENT) {
 
381
            snprintf(error_buf, sizeof(error_buf),
 
382
                     "Error while stating file %s, errno = %d", filename,
 
383
                     errno);
 
384
            PopupWarning(man_globals, error_buf);
 
385
        }
 
386
        return (FALSE);
376
387
    }
377
 
    return(FALSE);
378
 
  }
379
388
 
380
389
/*
381
 
 * Using stdin is necessary to fool zcat since we cannot guarentee
 
390
 * Using stdin is necessary to fool zcat since we cannot guarantee
382
391
 * the .Z extension.
383
392
 */
384
393
 
385
 
  strcpy(tmp, MANTEMP);         /* get a temp file. */
386
 
#ifndef HAS_MKSTEMP
387
 
  (void) mktemp(tmp);
388
 
#else
389
 
  fd = mkstemp(tmp);
390
 
  if (fd < 0) {
391
 
      PopupWarning(man_globals, "Error creating a temp file");
392
 
      return FALSE;
393
 
  }
394
 
  *output_fd = fdopen(fd, "r");
395
 
#endif
396
 
  strcpy(output, tmp);
 
394
    strcpy(tmp, MANTEMP);       /* get a temp file. */
 
395
    fd = mkstemp(tmp);
 
396
    if (fd < 0) {
 
397
        PopupWarning(man_globals, "Error creating a temp file");
 
398
        return FALSE;
 
399
    }
 
400
    *output_file = fdopen(fd, "r");
 
401
    if (*output_file == NULL) {
 
402
        remove(tmp);
 
403
        close(fd);
 
404
        PopupWarning(man_globals, "Error opening temp file");
 
405
        return FALSE;
 
406
    }
 
407
    strcpy(output, tmp);
397
408
 
398
409
#ifdef GZIP_EXTENSION
399
 
  if (streq(filename + strlen(filename) - strlen(GZIP_EXTENSION),
400
 
            GZIP_EXTENSION))
401
 
    snprintf(cmdbuf, sizeof(cmdbuf), GUNZIP_FORMAT, filename, output);
402
 
  else
 
410
    if (streq(filename + strlen(filename) - strlen(GZIP_EXTENSION),
 
411
              GZIP_EXTENSION))
 
412
        snprintf(cmdbuf, sizeof(cmdbuf), GUNZIP_FORMAT, filename, output);
 
413
    else
403
414
#endif
404
415
#ifdef BZIP2_EXTENSION
405
 
  if (streq(filename + strlen(filename) - strlen(BZIP2_EXTENSION),
406
 
            BZIP2_EXTENSION))
407
 
    snprintf(cmdbuf, sizeof(cmdbuf), BUNZIP2_FORMAT, filename, output);
408
 
  else
 
416
    if (streq(filename + strlen(filename) - strlen(BZIP2_EXTENSION),
 
417
                  BZIP2_EXTENSION))
 
418
        snprintf(cmdbuf, sizeof(cmdbuf), BUNZIP2_FORMAT, filename, output);
 
419
    else
409
420
#endif
410
421
#ifdef LZMA_EXTENSION
411
 
  if (streq(filename + strlen(filename) - strlen(LZMA_EXTENSION),
412
 
            LZMA_EXTENSION))
413
 
    snprintf(cmdbuf, sizeof(cmdbuf), UNLZMA_FORMAT, filename, output);
414
 
  else
 
422
    if (streq(filename + strlen(filename) - strlen(LZMA_EXTENSION),
 
423
                  LZMA_EXTENSION))
 
424
        snprintf(cmdbuf, sizeof(cmdbuf), UNLZMA_FORMAT, filename, output);
 
425
    else
415
426
#endif
416
 
  snprintf(cmdbuf, sizeof(cmdbuf), UNCOMPRESS_FORMAT, filename, output);
417
 
  if(system(cmdbuf) == 0)       /* execute search. */
418
 
    return(TRUE);
 
427
        snprintf(cmdbuf, sizeof(cmdbuf), UNCOMPRESS_FORMAT, filename, output);
 
428
    if (system(cmdbuf) == 0)    /* execute search. */
 
429
        return (TRUE);
419
430
 
420
 
  snprintf(error_buf, sizeof(error_buf),
421
 
           "Error while uncompressing, command was: %s", cmdbuf);
422
 
  PopupWarning(man_globals, error_buf);
423
 
  return(FALSE);
 
431
    snprintf(error_buf, sizeof(error_buf),
 
432
             "Error while uncompressing, command was: %s", cmdbuf);
 
433
    PopupWarning(man_globals, error_buf);
 
434
    return (FALSE);
424
435
}
425
436
 
426
437
#if defined(SMAN) && defined(SFORMAT)
427
438
/*      Function Name: SgmlToRoffNamed
428
439
 *      Description: This function will attempt to find an SGML man
429
440
 *                   page and convert it to roff format.
430
 
 *      Arguments: man_globals - the psuedo global info.
 
441
 *      Arguments: man_globals - the pseudo global info.
431
442
 *                 filename - name of file to uncompress.
432
443
 * RETURNED        output - the file name output (must be an allocated string).
433
444
 *      Returns:; TRUE if the file was found.
434
445
 */
435
446
 
436
 
#ifndef HAS_MKSTEMP
437
 
static Boolean
438
 
SgmlToRoffNamed(ManpageGlobals * man_globals, char * filename, char * output)
439
 
#else
440
 
static Boolean
441
 
SgmlToRoffNamed(ManpageGlobals * man_globals, char * filename, char * output,
442
 
                FILE ** output_fd)
443
 
#endif
 
447
static Boolean
 
448
SgmlToRoffNamed(ManpageGlobals * man_globals, char *filename, char *output,
 
449
                FILE ** output_file)
444
450
{
445
 
  char tmp[BUFSIZ], cmdbuf[BUFSIZ], error_buf[BUFSIZ];
446
 
  struct stat junk;
447
 
#ifdef HAS_MKSTEMP
448
 
  int fd;
449
 
#endif
450
 
 
451
 
  if (stat(filename, &junk) != 0) { /* Check for existance of the file. */
452
 
    if (errno != ENOENT) {
453
 
      snprintf(error_buf, sizeof(error_buf),
454
 
               "Error while stating file %s, errno = %d", filename, errno);
455
 
      PopupWarning(man_globals, error_buf);
456
 
    }
457
 
    return(FALSE);
458
 
  }
459
 
 
460
 
  strcpy(tmp, MANTEMP);         /* get a temp file. */
461
 
#ifndef HAS_MKSTEMP
462
 
  (void) mktemp(tmp);
463
 
#else
464
 
  fd = mkstemp(tmp);
465
 
  if (fd < 0) {
466
 
      PopupWarning(man_globals, "Error creating a temp file");
467
 
      return FALSE;
468
 
  }
469
 
  *output_fd = fdopen(fd, "r");
470
 
#endif
471
 
  strcpy(output, tmp);
472
 
 
473
 
  snprintf(cmdbuf, sizeof(cmdbuf), "%s %s > %s", SFORMAT, filename, output);
474
 
  if(system(cmdbuf) == 0)       /* execute search. */
475
 
    return(TRUE);
476
 
 
477
 
  snprintf(error_buf, sizeof(error_buf),
478
 
           "Error while converting from sgml, command was: %s", cmdbuf);
479
 
  PopupWarning(man_globals, error_buf);
480
 
  return(FALSE);
 
451
    char tmp[BUFSIZ], cmdbuf[BUFSIZ], error_buf[BUFSIZ];
 
452
    struct stat junk;
 
453
    int fd;
 
454
 
 
455
    if (stat(filename, &junk) != 0) {   /* Check for existence of the file. */
 
456
        if (errno != ENOENT) {
 
457
            snprintf(error_buf, sizeof(error_buf),
 
458
                     "Error while stating file %s, errno = %d", filename,
 
459
                     errno);
 
460
            PopupWarning(man_globals, error_buf);
 
461
        }
 
462
        return (FALSE);
 
463
    }
 
464
 
 
465
    strcpy(tmp, MANTEMP);       /* get a temp file. */
 
466
    fd = mkstemp(tmp);
 
467
    if (fd < 0) {
 
468
        PopupWarning(man_globals, "Error creating a temp file");
 
469
        return FALSE;
 
470
    }
 
471
    *output_file = fdopen(fd, "r");
 
472
    if (*output_file == NULL) {
 
473
        remove(tmp);
 
474
        close(fd);
 
475
        PopupWarning(man_globals, "Error opening temp file");
 
476
        return FALSE;
 
477
    }
 
478
    strcpy(output, tmp);
 
479
 
 
480
    snprintf(cmdbuf, sizeof(cmdbuf), "%s %s >> %s", SFORMAT, filename, output);
 
481
    if (system(cmdbuf) == 0)    /* execute search. */
 
482
        return (TRUE);
 
483
 
 
484
    snprintf(error_buf, sizeof(error_buf),
 
485
             "Error while converting from sgml, command was: %s", cmdbuf);
 
486
    PopupWarning(man_globals, error_buf);
 
487
    return (FALSE);
481
488
}
482
 
#endif /* defined (SMAN) && defined(SFORMAT) */
 
489
#endif                          /* defined (SMAN) && defined(SFORMAT) */
483
490
 
484
491
/*      Function Name: Format
485
 
 *      Description: This funtion formats the manual pages and interfaces
 
492
 *      Description: This function formats the manual pages and interfaces
486
493
 *                   with the user.
487
 
 *      Arguments: man_globals - the psuedo globals
 
494
 *      Arguments: man_globals - the pseudo globals
488
495
 *                 file - the file pointer to use and return
489
496
 *                 entry - the current entry struct.
490
497
 *                 current_box - The current directory being displayed.
492
499
 */
493
500
 
494
501
/* ARGSUSED */
495
 
 
496
502
FILE *
497
 
Format(ManpageGlobals * man_globals, char * entry)
 
503
Format(ManpageGlobals * man_globals, const char *entry)
498
504
{
499
 
  FILE * file = NULL;
500
 
#ifdef HAS_MKSTEMP
501
 
  int fd;
502
 
#endif
503
 
  Widget manpage = man_globals->manpagewidgets.manpage;
504
 
  char cmdbuf[BUFSIZ], tmp[BUFSIZ], filename[BUFSIZ], error_buf[BUFSIZ];
505
 
  char path[BUFSIZ], sect[BUFSIZ];
506
 
  XEvent event;
507
 
  Position x,y;                 /* location to pop up the
508
 
                                   "would you like to save" widget. */
509
 
 
510
 
#ifndef HAS_MKSTEMP
511
 
  if ( !UncompressUnformatted(man_globals, entry, filename) ) {
512
 
#else
513
 
  if ( !UncompressUnformatted(man_globals, entry, filename, &file) ) {
514
 
#endif
515
 
    /* We Really could not find it, this should never happen, yea right. */
516
 
    snprintf(error_buf, sizeof(error_buf),
517
 
             "Could not open manual page, %s", entry);
518
 
    PopupWarning(man_globals, error_buf);
519
 
    XtPopdown( XtParent(man_globals->standby) );
520
 
    return(NULL);
521
 
  }
522
 
 
523
 
#ifndef HAS_MKSTEMP
524
 
  if ((file = fopen(filename, "r")) != NULL) {
525
 
#else
526
 
  if (file != NULL) {
527
 
#endif
528
 
    char line[BUFSIZ];
529
 
 
530
 
    if (fgets(line, sizeof(line), file) != NULL) {
531
 
        if (strncmp(line, ".so ", 4) == 0) {
532
 
          line[strlen(line) - 1] = '\0';
533
 
          fclose(file);
534
 
          unlink(filename);
535
 
          if (line[4] != '/') {
536
 
            char *ptr = NULL;
537
 
 
538
 
            strcpy(tmp, entry);
539
 
            if ((ptr = rindex(tmp, '/')) != NULL) {
540
 
              *ptr = '\0';
541
 
              if ((ptr = rindex(tmp, '/')) != NULL)
542
 
                ptr[1] = '\0';
543
 
            }
544
 
          }
545
 
          else
546
 
            *tmp = '\0';
547
 
          snprintf(filename, sizeof(filename), "%s%s", tmp, line + 4);
548
 
 
549
 
          return (Format(man_globals, filename));
550
 
        }
551
 
    }
552
 
    fclose(file);
553
 
  }
554
 
 
555
 
  Popup(XtParent(man_globals->standby), XtGrabExclusive);
556
 
  while ( !XCheckTypedWindowEvent(XtDisplay(man_globals->standby),
557
 
                                  XtWindow(man_globals->standby),
558
 
                                  Expose, &event) );
559
 
  XtDispatchEvent( &event );
560
 
  XFlush(XtDisplay(man_globals->standby));
561
 
 
562
 
  strcpy(tmp,MANTEMP);                    /* Get a temp file. */
563
 
#ifndef HAS_MKSTEMP
564
 
  (void) mktemp(tmp);
565
 
#else
566
 
  fd = mkstemp(tmp);
567
 
  file = fdopen(fd, "r");
568
 
#endif
569
 
  strcpy(man_globals->tempfile, tmp);
570
 
 
571
 
  ParseEntry(entry, path, sect, NULL);
 
505
    FILE *file = NULL;
 
506
    int fd;
 
507
 
 
508
    Widget manpage = man_globals->manpagewidgets.manpage;
 
509
    char cmdbuf[BUFSIZ], tmp[BUFSIZ], filename[BUFSIZ], error_buf[BUFSIZ];
 
510
    char path[BUFSIZ], sect[BUFSIZ];
 
511
    XEvent event;
 
512
    Position x, y;              /* location to pop up the
 
513
                                   "would you like to save" widget. */
 
514
 
 
515
    if (!UncompressUnformatted(man_globals, entry, filename, &file)) {
 
516
        /* We Really could not find it, this should never happen, yea right. */
 
517
        snprintf(error_buf, sizeof(error_buf),
 
518
                 "Could not open manual page, %s", entry);
 
519
        PopupWarning(man_globals, error_buf);
 
520
        XtPopdown(XtParent(man_globals->standby));
 
521
        return (NULL);
 
522
    }
 
523
 
 
524
    if (file != NULL) {
 
525
        char line[BUFSIZ];
 
526
 
 
527
        if (fgets(line, sizeof(line), file) != NULL) {
 
528
            if (strncmp(line, ".so ", 4) == 0) {
 
529
                line[strlen(line) - 1] = '\0';
 
530
                fclose(file);
 
531
                remove(filename);
 
532
                if (line[4] != '/') {
 
533
                    char *ptr = NULL;
 
534
 
 
535
                    strcpy(tmp, entry);
 
536
                    if ((ptr = strrchr(tmp, '/')) != NULL) {
 
537
                        *ptr = '\0';
 
538
                        if ((ptr = strrchr(tmp, '/')) != NULL)
 
539
                            ptr[1] = '\0';
 
540
                    }
 
541
                }
 
542
                else
 
543
                    *tmp = '\0';
 
544
                snprintf(filename, sizeof(filename), "%s%s", tmp, line + 4);
 
545
 
 
546
                return (Format(man_globals, filename));
 
547
            }
 
548
        }
 
549
        fclose(file);
 
550
    }
 
551
 
 
552
    Popup(XtParent(man_globals->standby), XtGrabExclusive);
 
553
    while (!XCheckTypedWindowEvent(XtDisplay(man_globals->standby),
 
554
                                   XtWindow(man_globals->standby),
 
555
                                   Expose, &event));
 
556
    XtDispatchEvent(&event);
 
557
    XFlush(XtDisplay(man_globals->standby));
 
558
 
 
559
    strcpy(tmp, MANTEMP);       /* Get a temp file. */
 
560
    fd = mkstemp(tmp);
 
561
    if (fd >= 0) {
 
562
        file = fdopen(fd, "r");
 
563
        if (file == NULL) {
 
564
            remove(tmp);
 
565
            close(fd);
 
566
        }
 
567
    }
 
568
    else
 
569
        file = NULL;
 
570
    if (file == NULL) {
 
571
        PopupWarning(man_globals, "Something went wrong in opening the "
 
572
                     "temp file, try cleaning up /tmp");
 
573
        return NULL;
 
574
    }
 
575
    strcpy(man_globals->tempfile, tmp);
 
576
 
 
577
    ParseEntry(entry, path, sect, NULL);
572
578
 
573
579
#ifndef HANDLE_ROFFSEQ
574
 
#ifndef HAS_MKSTEMP
575
 
  snprintf(cmdbuf, sizeof(cmdbuf), "cd %s ; %s %s %s > %s %s", path, TBL,
576
 
          filename, FORMAT, man_globals->tempfile, "2> /dev/null");
577
 
#else
578
 
  snprintf(cmdbuf, sizeof(cmdbuf), "cd %s ; %s %s %s >> %s %s", path, TBL,
579
 
          filename, FORMAT, man_globals->tempfile, "2> /dev/null");
580
 
#endif
581
 
#else
582
 
  /* Handle more flexible way of specifying the formatting pipeline */
583
 
  if (! ConstructCommand(cmdbuf, path, filename, man_globals->tempfile)) {
584
 
     PopupWarning(man_globals, "Constructed command was too long!");
585
 
     file = NULL;
586
 
  }
587
 
  else
588
 
#endif /* HANDLE_ROFFSEQ */
 
580
    snprintf(cmdbuf, sizeof(cmdbuf), "cd %s ; %s %s %s >> %s %s", path, TBL,
 
581
             filename, FORMAT, man_globals->tempfile, "2> /dev/null");
 
582
#else
 
583
    /* Handle more flexible way of specifying the formatting pipeline */
 
584
    if (!ConstructCommand(cmdbuf, path, filename, man_globals->tempfile)) {
 
585
        PopupWarning(man_globals, "Constructed command was too long!");
 
586
        fclose(file);
 
587
        file = NULL;
 
588
    }
 
589
    else
 
590
#endif                          /* HANDLE_ROFFSEQ */
589
591
 
590
 
  if(system(cmdbuf) != 0) {     /* execute search. */
591
 
    snprintf(error_buf, sizeof(error_buf),
592
 
            "Something went wrong trying to run the command: %s", cmdbuf);
593
 
    PopupWarning(man_globals, error_buf);
594
 
    file = NULL;
595
 
  }
596
 
  else {
597
 
#ifndef HAS_MKSTEMP
598
 
    if ((file = fopen(man_globals->tempfile,"r")) == NULL) {
599
 
      PopupWarning(man_globals, "Something went wrong in retrieving the "
600
 
                   "temp file, try cleaning up /tmp");
 
592
    if (system(cmdbuf) != 0) {  /* execute search. */
 
593
        snprintf(error_buf, sizeof(error_buf),
 
594
                 "Something went wrong trying to run the command: %s", cmdbuf);
 
595
        PopupWarning(man_globals, error_buf);
 
596
        fclose(file);
 
597
        file = NULL;
601
598
    }
602
599
    else {
603
 
#endif
604
 
 
605
 
      XtPopdown( XtParent(man_globals->standby) );
606
 
 
607
 
      if ( (man_globals->save == NULL) ||
608
 
           (man_globals->manpagewidgets.manpage == NULL) )
609
 
        unlink(man_globals->tempfile);
610
 
      else {
611
 
        char * ptr, catdir[BUFSIZ];
612
 
 
613
 
        /*
614
 
         * If the catdir is writeable then ask the user if he/she wants to
615
 
         * write the man page to it.
616
 
         */
617
 
 
618
 
        strcpy(catdir, man_globals->save_file);
619
 
        if ( (ptr = rindex(catdir, '/')) != NULL) {
620
 
          *ptr = '\0';
621
 
 
622
 
          if ( access(catdir, W_OK) != 0 )
623
 
            unlink(man_globals->tempfile);
624
 
          else {
625
 
            x = (Position) Width(man_globals->manpagewidgets.manpage)/2;
626
 
            y = (Position) Height(man_globals->manpagewidgets.manpage)/2;
627
 
            XtTranslateCoords(manpage, x, y, &x, &y);
628
 
            PositionCenter( man_globals->save, (int) x, (int) y, 0, 0, 0, 0);
629
 
            XtPopup( man_globals->save, XtGrabExclusive);
630
 
          }
631
 
        }
632
 
        else
633
 
          unlink(man_globals->tempfile);
634
 
      }
635
 
#ifndef HAS_MKSTEMP
 
600
        if (file != NULL) {
 
601
            XtPopdown(XtParent(man_globals->standby));
 
602
 
 
603
            if ((man_globals->save == NULL) ||
 
604
                (man_globals->manpagewidgets.manpage == NULL))
 
605
                remove(man_globals->tempfile);
 
606
            else {
 
607
                char *ptr, catdir[BUFSIZ];
 
608
 
 
609
                /*
 
610
                 * If the catdir is writable then ask the user if he/she wants to
 
611
                 * write the man page to it.
 
612
                 */
 
613
 
 
614
                strcpy(catdir, man_globals->save_file);
 
615
                if ((ptr = strrchr(catdir, '/')) != NULL) {
 
616
                    *ptr = '\0';
 
617
 
 
618
                    if (access(catdir, W_OK) != 0)
 
619
                        remove(man_globals->tempfile);
 
620
                    else {
 
621
                        x = (Position) Width(man_globals->manpagewidgets.
 
622
                                             manpage) / 2;
 
623
                        y = (Position) Height(man_globals->manpagewidgets.
 
624
                                              manpage) / 2;
 
625
                        XtTranslateCoords(manpage, x, y, &x, &y);
 
626
                        PositionCenter(man_globals->save, (int) x, (int) y, 0,
 
627
                                       0, 0, 0);
 
628
                        XtPopup(man_globals->save, XtGrabExclusive);
 
629
                    }
 
630
                }
 
631
                else
 
632
                    remove(man_globals->tempfile);
 
633
            }
 
634
        }
636
635
    }
637
 
#endif
638
 
  }
639
 
 
640
 
 /*
641
 
  * If the original was compressed or in another format, delete temporary file.
642
 
  */
643
 
  if (man_globals->deletetempfile) 
644
 
    unlink(filename);
645
 
 
646
 
  return(file);
 
636
 
 
637
    /*
 
638
     * If the original was compressed or in another format, delete temporary file.
 
639
     */
 
640
    if (man_globals->deletetempfile)
 
641
        remove(filename);
 
642
 
 
643
    return (file);
647
644
}
648
645
 
649
646
#ifdef HANDLE_ROFFSEQ
659
656
 *               be too long (more than BUFSIZ characters)
660
657
 */
661
658
static Boolean
662
 
ConstructCommand(cmdbuf, path, filename, tempfile)
663
 
   char *cmdbuf, *path, *filename, *tempfile;
 
659
ConstructCommand(char *cmdbuf, const char *path,
 
660
                 const char *filename, const char *tempfile)
664
661
{
665
 
   /* The original code did the following to produce a command line:
666
 
    *   sprintf(cmdbuf,"cd %s ; %s %s %s > %s %s", path, TBL,
667
 
    *      filename, FORMAT, man_globals->tempfile, "2> /dev/null");
668
 
    * We are more flexible and follow more or less the algorithm used
669
 
    * by the Linux man command:
670
 
    *  + Obtain a string of letters from the following sources in order
671
 
    *    of preference:
672
 
    *    + a command line option (not implemented in xman; it's probably not
673
 
    *      useful)
674
 
    *    + the first line of the manpage source, if it is of the form:
675
 
    *      '\" <string>
676
 
    *    + the MANROFFSEQ environment variable
677
 
    *    + a default string; this is "".
678
 
    *  + Interpret the string as a pipeline of filters:
679
 
    *    + e = eqn   g = grap   p = pic   t = tbl   v = vgrind   r = refer
680
 
    *  + zsoelim is always run as the first preprocessor in any case.
681
 
    *
682
 
    * Strictly speaking we should save a catpage iff the string comes
683
 
    * from the file or is the default.
684
 
    *
685
 
    * You'll notice that we format a man page into ASCII text output and then
686
 
    * attempt to interpret things like L^HL as bold and so forth. This
687
 
    * is so obviously the Wrong Thing it's untrue.
688
 
    */
689
 
   char *c = cmdbuf;           /* current posn in buffer */
690
 
   int left = BUFSIZ;          /* space left in buffer */
691
 
   int used;
692
 
   char *fmt;
693
 
   FILE *file;
694
 
   char fmtbuf[128];
695
 
   int gotfmt = 0;             /* set to 1 if we got a directive from source */
696
 
   char fname[PATH_MAX];
697
 
#ifdef __UNIXOS2__
698
 
   int i;
699
 
#endif
700
 
 
701
 
   fmt = NULL;
702
 
   /* If you have a command line option that gives a setting for fmt,
703
 
      set it here. */
704
 
 
705
 
   if (!fmt) {
706
 
      /* This is the tricky bit: extract a format string from the source file
707
 
       * Annoyingly, filename might be relative or absolute. We cheat and
708
 
       * use system to get the thing to a known absoute filename.
709
 
       */
710
 
      if (filename[0] == '/') {
711
 
         snprintf(fname, sizeof(fname), "%s", filename);
712
 
      } else {
713
 
         snprintf(fname, sizeof(fname), "%s/%s", path, filename);
714
 
      }
715
 
      if ((file = fopen(fname, "r")) &&
716
 
          (fgets(fmtbuf, sizeof(fmtbuf), file)) &&
717
 
          (!memcmp(fmtbuf, "'\\\" ", 4))) {
718
 
                              /* that's squote-backslash-dquote-space */
719
 
         int len;
720
 
         fmt = fmtbuf + 3;
721
 
         len = strlen(fmt);
722
 
         if (len && (fmt[len-1] == '\n')) {
723
 
            fmt[len-1] = 0;
724
 
            gotfmt++;
725
 
         }
726
 
      }
727
 
      if (!gotfmt)                                /* not there or some error */
728
 
      {
729
 
         fmt = getenv("MANROFFSEQ");
730
 
      }
731
 
   }
732
 
 
733
 
   if (!fmt)
734
 
   {
735
 
      fmt = DEFAULT_MANROFFSEQ;
736
 
   }
737
 
 
738
 
 
739
 
   /* Start with the first fixed part of the command line */
740
 
#ifdef __UNIXOS2__
741
 
   for (i = 0; i < strlen(path); i++) {
742
 
     if (path[i] == '/')
743
 
       path[i] = '\\';
744
 
   }
745
 
   used = snprintf(c, left, "cd %s & %s %s ", path, ZSOELIM, filename);
746
 
#else
747
 
   used = snprintf(c, left, "cd %s; %s %s ", path, ZSOELIM, filename);
748
 
#endif
749
 
   left -= used;
750
 
   c += used;
751
 
   if (left <= 1)
752
 
      return (FALSE);
753
 
 
754
 
   /* Now add preprocessors of the form '| processor' */
755
 
   for ( ; *fmt; fmt++)
756
 
   {
757
 
      char *filter;
758
 
      switch (*fmt)
759
 
      {
760
 
         case 'e':
 
662
    /* The original code did the following to produce a command line:
 
663
     *   sprintf(cmdbuf,"cd %s ; %s %s %s > %s %s", path, TBL,
 
664
     *      filename, FORMAT, man_globals->tempfile, "2> /dev/null");
 
665
     * We are more flexible and follow more or less the algorithm used
 
666
     * by the Linux man command:
 
667
     *  + Obtain a string of letters from the following sources in order
 
668
     *    of preference:
 
669
     *    + a command line option (not implemented in xman; it's probably not
 
670
     *      useful)
 
671
     *    + the first line of the manpage source, if it is of the form:
 
672
     *      '\" <string>
 
673
     *    + the MANROFFSEQ environment variable
 
674
     *    + a default string; this is "".
 
675
     *  + Interpret the string as a pipeline of filters:
 
676
     *    + e = eqn   g = grap   p = pic   t = tbl   v = vgrind   r = refer
 
677
     *  + zsoelim is always run as the first preprocessor in any case.
 
678
     *
 
679
     * Strictly speaking we should save a catpage iff the string comes
 
680
     * from the file or is the default.
 
681
     *
 
682
     * You'll notice that we format a man page into ASCII text output and then
 
683
     * attempt to interpret things like L^HL as bold and so forth. This
 
684
     * is so obviously the Wrong Thing it's untrue.
 
685
     */
 
686
    char *c = cmdbuf;           /* current posn in buffer */
 
687
    int left = BUFSIZ;          /* space left in buffer */
 
688
    int used;
 
689
    const char *fmt;
 
690
    char fmtbuf[128];
 
691
 
 
692
    fmt = NULL;
 
693
    /* If you have a command line option that gives a setting for fmt,
 
694
       set it here. */
 
695
 
 
696
    if (!fmt) {
 
697
        /* This is the tricky bit: extract a format string from the source file
 
698
         * Annoyingly, filename might be relative or absolute. We cheat and
 
699
         * use system to get the thing to a known absolute filename.
 
700
         */
 
701
        FILE *file;
 
702
        int gotfmt = 0;    /* set to 1 if we got a directive from source */
 
703
        char fname[PATH_MAX];
 
704
 
 
705
        if (filename[0] == '/') {
 
706
            snprintf(fname, sizeof(fname), "%s", filename);
 
707
        }
 
708
        else {
 
709
            snprintf(fname, sizeof(fname), "%s/%s", path, filename);
 
710
        }
 
711
        if ((file = fopen(fname, "r")) != NULL) {
 
712
            if ((fgets(fmtbuf, sizeof(fmtbuf), file)) &&
 
713
                (!memcmp(fmtbuf, "'\\\" ", 4))) {
 
714
                /* that's squote-backslash-dquote-space */
 
715
                int len = strlen(fmtbuf);
 
716
 
 
717
                if (len && (fmtbuf[len - 1] == '\n')) {
 
718
                    fmtbuf[len - 1] = 0;
 
719
                    fmt = fmtbuf + 3;
 
720
                    gotfmt++;
 
721
                }
 
722
            }
 
723
            fclose(file);
 
724
        }
 
725
        if (!gotfmt) {          /* not there or some error */
 
726
            fmt = getenv("MANROFFSEQ");
 
727
        }
 
728
    }
 
729
 
 
730
    if (!fmt) {
 
731
        fmt = DEFAULT_MANROFFSEQ;
 
732
    }
 
733
 
 
734
    /* Start with the first fixed part of the command line */
 
735
    used = snprintf(c, left, "cd %s; %s %s ", path, ZSOELIM, filename);
 
736
    left -= used;
 
737
    c += used;
 
738
    if (left <= 1)
 
739
        return (FALSE);
 
740
 
 
741
    /* Now add preprocessors of the form '| processor' */
 
742
    for (; *fmt; fmt++) {
 
743
        const char *filter;
 
744
 
 
745
        switch (*fmt) {
 
746
        case 'e':
761
747
            filter = EQN;
762
748
            break;
763
 
         case 'g':
 
749
        case 'g':
764
750
            filter = GRAP;
765
751
            break;
766
 
         case 'p':
767
 
            filter = PIC;
 
752
        case 'p':
 
753
            filter = ROFF_PIC;
768
754
            break;
769
 
         case 't':
 
755
        case 't':
770
756
            filter = TBL;
771
757
            break;
772
 
         case 'v':
 
758
        case 'v':
773
759
            filter = VGRIND;
774
760
            break;
775
 
         case 'r':
 
761
        case 'r':
776
762
            filter = REFER;
777
763
            break;
778
 
         default:
 
764
        default:
779
765
            filter = NULL;
780
766
            break;
781
 
      }
782
 
      if (filter)
783
 
      {
784
 
         used = snprintf(c, left, " | %s ", filter);
785
 
         left -= used;
786
 
         c += used;
787
 
         if (left <= 1)
788
 
            return (FALSE);
789
 
      }
790
 
   }
791
 
 
792
 
   /* Now add the fixed trailing part 'formatprog > tempfile 2> /dev/null' */
793
 
#ifdef __UNIXOS2__
794
 
   used = snprintf(c, left, " | %s > %s 2>NUL", FORMAT, tempfile);
795
 
#else
796
 
#ifndef HAS_MKSTEMP
797
 
   used = snprintf(c, left, " | %s > %s 2>/dev/null", FORMAT, tempfile);
798
 
#else
799
 
   used = snprintf(c, left, " | %s >> %s 2>/dev/null", FORMAT, tempfile);
800
 
#endif
801
 
#endif /* __UNIXOS2__ */
802
 
   left -= used;
803
 
   if (left <= 1)
804
 
      return (FALSE);
805
 
 
806
 
   return (TRUE);
 
767
        }
 
768
        if (filter) {
 
769
            used = snprintf(c, left, " | %s ", filter);
 
770
            left -= used;
 
771
            c += used;
 
772
            if (left <= 1)
 
773
                return (FALSE);
 
774
        }
 
775
    }
 
776
 
 
777
    /* Now add the fixed trailing part 'formatprog > tempfile 2> /dev/null' */
 
778
    used = snprintf(c, left, " | %s >> %s 2>/dev/null", FORMAT, tempfile);
 
779
    left -= used;
 
780
    if (left <= 1)
 
781
        return (FALSE);
 
782
 
 
783
    return (TRUE);
807
784
}
808
 
#endif /* HANDLE_ROFFSEQ */
 
785
#endif                          /* HANDLE_ROFFSEQ */
809
786
 
810
787
/*      Function Name: UncompressUnformatted
811
788
 *      Description: Finds an uncompressed unformatted manual page.
812
 
 *      Arguments: man_globals - the psuedo global structure.
 
789
 *      Arguments: man_globals - the pseudo global structure.
813
790
 *                 entry - the manual page entry.
814
791
 * RETURNED        filename - location to put the name of the file.
815
792
 *      Returns: TRUE if the file was found.
816
793
 */
817
794
 
818
795
static Boolean
819
 
#ifndef HAS_MKSTEMP
820
 
UncompressUnformatted(ManpageGlobals * man_globals, char * entry,
821
 
                      char * filename)
822
 
#else
823
 
UncompressUnformatted(ManpageGlobals * man_globals, char * entry,
824
 
                      char * filename, FILE **file)
825
 
#endif
 
796
UncompressUnformatted(ManpageGlobals * man_globals, const char *entry,
 
797
                      char *filename, FILE ** file)
826
798
{
827
 
  char path[BUFSIZ], page[BUFSIZ], section[BUFSIZ], input[BUFSIZ];
828
 
  int len_cat = strlen(CAT), len_man = strlen(MAN);
 
799
    char path[BUFSIZ], page[BUFSIZ], section[BUFSIZ], input[BUFSIZ];
 
800
    int len_cat = strlen(CAT), len_man = strlen(MAN);
 
801
 
829
802
#if defined(SMAN) && defined(SFORMAT)
830
 
  int len_sman = strlen(SMAN);
 
803
    int len_sman = strlen(SMAN);
831
804
#endif
832
805
 
833
 
  ParseEntry(entry, path, section, page);
 
806
    ParseEntry(entry, path, section, page);
834
807
 
835
 
  man_globals->bzip2 = FALSE;
836
 
  man_globals->lzma = FALSE;
 
808
    man_globals->bzip2 = FALSE;
 
809
    man_globals->lzma = FALSE;
837
810
 
838
811
#if defined(__OpenBSD__) || defined(__NetBSD__)
839
 
  /*
840
 
   * look for uncompressed file in machine subdir first
841
 
   */
842
 
  snprintf(filename, BUFSIZ, "%s/%s%s/%s/%s", path, MAN,
843
 
          section + len_cat, MACHINE, page);
844
 
  if ( access( filename, R_OK ) == 0 ) {
845
 
    man_globals->compress = FALSE;
846
 
    man_globals->gzip = FALSE;
847
 
    man_globals->deletetempfile = FALSE;
848
 
    snprintf(man_globals->save_file, sizeof(man_globals->save_file),
849
 
             "%s/%s%s/%s/%s", path, CAT, section + len_cat, MACHINE, page);
850
 
    return(TRUE);
851
 
  }
852
 
 /*
853
 
  * Then for compressed files in an uncompressed directory.
854
 
  */
855
 
  snprintf(input, sizeof(input), "%s.%s", filename, COMPRESSION_EXTENSION);
856
 
#ifndef HAS_MKSTEMP
857
 
  if ( UncompressNamed(man_globals, input, filename) ) {
858
 
#else
859
 
  if ( UncompressNamed(man_globals, input, filename, file) ) {
860
 
#endif
861
 
    man_globals->compress = TRUE;
862
 
    man_globals->deletetempfile = TRUE;
863
 
    snprintf(man_globals->save_file, sizeof(man_globals->save_file),
864
 
             "%s/%s%s/%s.%s", path, CAT, section + len_cat, page, 
865
 
             COMPRESSION_EXTENSION);
866
 
    return(TRUE);
867
 
  }
 
812
    /*
 
813
     * look for uncompressed file in machine subdir first
 
814
     */
 
815
    snprintf(filename, BUFSIZ, "%s/%s%s/%s/%s", path, MAN,
 
816
             section + len_cat, MACHINE, page);
 
817
    if (access(filename, R_OK) == 0) {
 
818
        man_globals->compress = FALSE;
 
819
        man_globals->gzip = FALSE;
 
820
        man_globals->deletetempfile = FALSE;
 
821
        snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
822
                 "%s/%s%s/%s/%s", path, CAT, section + len_cat, MACHINE, page);
 
823
        return (TRUE);
 
824
    }
 
825
    /*
 
826
     * Then for compressed files in an uncompressed directory.
 
827
     */
 
828
    snprintf(input, sizeof(input), "%s.%s", filename, COMPRESSION_EXTENSION);
 
829
    if (UncompressNamed(man_globals, input, filename, file)) {
 
830
        man_globals->compress = TRUE;
 
831
        man_globals->deletetempfile = TRUE;
 
832
        snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
833
                 "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
834
                 COMPRESSION_EXTENSION);
 
835
        return (TRUE);
 
836
    }
868
837
#ifdef GZIP_EXTENSION
869
 
  else {
870
 
    snprintf(input, sizeof(input), "%s.%s", filename, GZIP_EXTENSION);
871
 
#ifndef HAS_MKSTEMP
872
 
    if ( UncompressNamed(man_globals, input, filename) ) {
873
 
#else
874
 
    if ( UncompressNamed(man_globals, input, filename, file) ) {
875
 
#endif
876
 
      man_globals->compress = TRUE;
877
 
      man_globals->gzip = TRUE;
878
 
      man_globals->deletetempfile = TRUE;
879
 
      snprintf(man_globals->save_file, sizeof(man_globals->save_file),
880
 
               "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
881
 
               GZIP_EXTENSION);
882
 
      return(TRUE);
 
838
    else {
 
839
        snprintf(input, sizeof(input), "%s.%s", filename, GZIP_EXTENSION);
 
840
        if (UncompressNamed(man_globals, input, filename, file)) {
 
841
            man_globals->compress = TRUE;
 
842
            man_globals->gzip = TRUE;
 
843
            man_globals->deletetempfile = TRUE;
 
844
            snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
845
                     "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
846
                     GZIP_EXTENSION);
 
847
            return (TRUE);
 
848
        }
883
849
    }
884
 
  }
885
 
#endif /* GZIP_EXTENSION */
886
 
#endif /* __OpenBSD__ || __NetBSD__ */
 
850
#endif                          /* GZIP_EXTENSION */
 
851
#endif                          /* __OpenBSD__ || __NetBSD__ */
887
852
 
888
853
#ifdef BZIP2_EXTENSION
889
 
 {
890
 
    snprintf(input, sizeof(input), "%s.%s", filename, BZIP2_EXTENSION);
891
 
#ifndef HAS_MKSTEMP
892
 
    if ( UncompressNamed(man_globals, input, filename) ) {
893
 
#else
894
 
    if ( UncompressNamed(man_globals, input, filename, file) ) {
895
 
#endif
896
 
      man_globals->compress = TRUE;
897
 
      man_globals->gzip = FALSE;
898
 
      man_globals->bzip2 = TRUE;
899
 
      snprintf(man_globals->save_file, sizeof(man_globals->save_file),
900
 
               "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
901
 
               BZIP2_EXTENSION);
902
 
      return(TRUE);
 
854
    {
 
855
        snprintf(input, sizeof(input), "%s.%s", filename, BZIP2_EXTENSION);
 
856
        if (UncompressNamed(man_globals, input, filename, file)) {
 
857
            man_globals->compress = TRUE;
 
858
            man_globals->gzip = FALSE;
 
859
            man_globals->bzip2 = TRUE;
 
860
            snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
861
                     "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
862
                     BZIP2_EXTENSION);
 
863
            return (TRUE);
 
864
        }
903
865
    }
904
 
  }
905
 
#endif /* BZIP2_EXTENSION */
 
866
#endif                          /* BZIP2_EXTENSION */
906
867
 
907
868
#ifdef LZMA_EXTENSION
908
 
 {
909
 
    snprintf(input, sizeof(input), "%s.%s", filename, LZMA_EXTENSION);
910
 
#ifndef HAS_MKSTEMP
911
 
    if ( UncompressNamed(man_globals, input, filename) ) {
912
 
#else
913
 
    if ( UncompressNamed(man_globals, input, filename, file) ) {
914
 
#endif
915
 
      man_globals->compress = TRUE;
916
 
      man_globals->gzip = FALSE;
917
 
      man_globals->lzma = TRUE;
918
 
      snprintf(man_globals->save_file, sizeof(man_globals->save_file),
919
 
               "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
920
 
               LZMA_EXTENSION);
921
 
      return(TRUE);
 
869
    {
 
870
        snprintf(input, sizeof(input), "%s.%s", filename, LZMA_EXTENSION);
 
871
        if (UncompressNamed(man_globals, input, filename, file)) {
 
872
            man_globals->compress = TRUE;
 
873
            man_globals->gzip = FALSE;
 
874
            man_globals->lzma = TRUE;
 
875
            snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
876
                     "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
877
                     LZMA_EXTENSION);
 
878
            return (TRUE);
 
879
        }
922
880
    }
923
 
  }
924
 
#endif /* LZMA_EXTENSION */
 
881
#endif                          /* LZMA_EXTENSION */
925
882
 
926
883
/*
927
884
 * Look for uncompressed file first.
928
885
 */
929
886
 
930
 
  snprintf(filename, BUFSIZ, "%s/%s%s/%s", path, MAN, section + len_man, page);
931
 
  if ( access( filename, R_OK ) == 0 ) {
932
 
    man_globals->compress = FALSE;
933
 
    man_globals->gzip = FALSE;
934
 
    man_globals->deletetempfile = FALSE;
935
 
    snprintf(man_globals->save_file, sizeof(man_globals->save_file), 
936
 
             "%s/%s%s/%s", path, CAT, section + len_cat, page);
937
 
    return(TRUE);
938
 
  }
 
887
    snprintf(filename, BUFSIZ, "%s/%s%s/%s", path, MAN, section + len_man,
 
888
             page);
 
889
    if (access(filename, R_OK) == 0) {
 
890
        man_globals->compress = FALSE;
 
891
        man_globals->gzip = FALSE;
 
892
        man_globals->deletetempfile = FALSE;
 
893
        snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
894
                 "%s/%s%s/%s", path, CAT, section + len_cat, page);
 
895
        return (TRUE);
 
896
    }
939
897
 
940
898
#if defined(SMAN) && defined(SFORMAT)
941
 
 /*
942
 
  * Look for uncompressed sgml file next.
943
 
  */
 
899
    /*
 
900
     * Look for uncompressed sgml file next.
 
901
     */
944
902
 
945
 
  snprintf(input, BUFSIZ, "%s/%s%s/%s", path, SMAN, section + len_sman, page);
946
 
#ifndef HAS_MKSTEMP
947
 
  if ( SgmlToRoffNamed(man_globals, input, filename) ) {
948
 
#else
949
 
  if ( SgmlToRoffNamed(man_globals, input, filename, file) ) {
950
 
#endif
951
 
    man_globals->compress = FALSE;
952
 
    man_globals->gzip = FALSE;
953
 
    man_globals->deletetempfile = TRUE;
954
 
    snprintf(man_globals->save_file, sizeof(man_globals->save_file),
955
 
            "%s/%s%s/%s", path, CAT, section + len_cat, page);
956
 
    return(TRUE);
957
 
  }
 
903
    snprintf(input, BUFSIZ, "%s/%s%s/%s", path, SMAN, section + len_sman, page);
 
904
    if (SgmlToRoffNamed(man_globals, input, filename, file)) {
 
905
        man_globals->compress = FALSE;
 
906
        man_globals->gzip = FALSE;
 
907
        man_globals->deletetempfile = TRUE;
 
908
        snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
909
                 "%s/%s%s/%s", path, CAT, section + len_cat, page);
 
910
        return (TRUE);
 
911
    }
958
912
#endif
959
913
 
960
914
/*
961
915
 * Then for compressed files in an uncompressed directory.
962
916
 */
963
917
 
964
 
  snprintf(input, sizeof(input), "%s.%s", filename, COMPRESSION_EXTENSION);
965
 
#ifndef HAS_MKSTEMP
966
 
  if ( UncompressNamed(man_globals, input, filename) ) {
967
 
#else
968
 
  if ( UncompressNamed(man_globals, input, filename, file) ) {
969
 
#endif
970
 
    man_globals->compress = TRUE;
971
 
    man_globals->deletetempfile = TRUE;
972
 
    snprintf(man_globals->save_file, sizeof(man_globals->save_file),
973
 
             "%s/%s%s/%s.%s", path, CAT, section + len_cat, page, 
974
 
             COMPRESSION_EXTENSION);
975
 
    return(TRUE);
976
 
  }
 
918
    snprintf(input, sizeof(input), "%s.%s", filename, COMPRESSION_EXTENSION);
 
919
    if (UncompressNamed(man_globals, input, filename, file)) {
 
920
        man_globals->compress = TRUE;
 
921
        man_globals->deletetempfile = TRUE;
 
922
        snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
923
                 "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
924
                 COMPRESSION_EXTENSION);
 
925
        return (TRUE);
 
926
    }
977
927
#ifdef GZIP_EXTENSION
978
 
  else {
979
 
    snprintf(input, sizeof(input), "%s.%s", filename, GZIP_EXTENSION);
980
 
#ifndef HAS_MKSTEMP
981
 
    if ( UncompressNamed(man_globals, input, filename) ) {
982
 
#else
983
 
    if ( UncompressNamed(man_globals, input, filename, file) ) {
984
 
#endif  
985
 
      man_globals->compress = TRUE;
986
 
      man_globals->gzip = TRUE;
987
 
      man_globals->deletetempfile = TRUE;
988
 
      snprintf(man_globals->save_file, sizeof(man_globals->save_file),
989
 
               "%s/%s%s/%s.%s", path, CAT, section + len_cat, page, 
990
 
               GZIP_EXTENSION);
991
 
      return(TRUE);
 
928
    else {
 
929
        snprintf(input, sizeof(input), "%s.%s", filename, GZIP_EXTENSION);
 
930
        if (UncompressNamed(man_globals, input, filename, file)) {
 
931
            man_globals->compress = TRUE;
 
932
            man_globals->gzip = TRUE;
 
933
            man_globals->deletetempfile = TRUE;
 
934
            snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
935
                     "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
936
                     GZIP_EXTENSION);
 
937
            return (TRUE);
 
938
        }
992
939
    }
993
 
  }
994
940
#endif
995
941
 
996
942
#ifdef BZIP2_EXTENSION
997
 
  {
998
 
    snprintf(input, sizeof(input), "%s.%s", filename, BZIP2_EXTENSION);
999
 
#ifndef HAS_MKSTEMP
1000
 
    if ( UncompressNamed(man_globals, input, filename) ) {
1001
 
#else
1002
 
    if ( UncompressNamed(man_globals, input, filename, file) ) {
1003
 
#endif  
1004
 
      man_globals->compress = TRUE;
1005
 
      man_globals->gzip = TRUE;
1006
 
      snprintf(man_globals->save_file, sizeof(man_globals->save_file),
1007
 
               "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
1008
 
               BZIP2_EXTENSION);
1009
 
      return(TRUE);
 
943
    {
 
944
        snprintf(input, sizeof(input), "%s.%s", filename, BZIP2_EXTENSION);
 
945
        if (UncompressNamed(man_globals, input, filename, file)) {
 
946
            man_globals->compress = TRUE;
 
947
            man_globals->gzip = TRUE;
 
948
            snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
949
                     "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
950
                     BZIP2_EXTENSION);
 
951
            return (TRUE);
 
952
        }
1010
953
    }
1011
 
  }
1012
954
#endif
1013
955
 
1014
956
#ifdef LZMA_EXTENSION
1015
 
  {
1016
 
    snprintf(input, sizeof(input), "%s.%s", filename, LZMA_EXTENSION);
1017
 
#ifndef HAS_MKSTEMP
1018
 
    if ( UncompressNamed(man_globals, input, filename) ) {
1019
 
#else
1020
 
    if ( UncompressNamed(man_globals, input, filename, file) ) {
1021
 
#endif  
1022
 
      man_globals->compress = TRUE;
1023
 
      man_globals->lzma = TRUE;
1024
 
      snprintf(man_globals->save_file, sizeof(man_globals->save_file),
1025
 
               "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
1026
 
               LZMA_EXTENSION);
1027
 
      return(TRUE);
 
957
    {
 
958
        snprintf(input, sizeof(input), "%s.%s", filename, LZMA_EXTENSION);
 
959
        if (UncompressNamed(man_globals, input, filename, file)) {
 
960
            man_globals->compress = TRUE;
 
961
            man_globals->lzma = TRUE;
 
962
            snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
963
                     "%s/%s%s/%s.%s", path, CAT, section + len_cat, page,
 
964
                     LZMA_EXTENSION);
 
965
            return (TRUE);
 
966
        }
1028
967
    }
1029
 
  }
1030
968
#endif
1031
969
 
1032
970
/*
1033
971
 * And lastly files in a compressed directory.
1034
972
 */
1035
973
 
1036
 
  snprintf(input, sizeof(input), "%s/%s%s.%s/%s", path,
1037
 
          MAN, section + len_man, COMPRESSION_EXTENSION, page);
1038
 
#ifndef HAS_MKSTEMP
1039
 
  if ( UncompressNamed(man_globals, input, filename) ) {
1040
 
#else
1041
 
  if ( UncompressNamed(man_globals, input, filename, file) ) {
1042
 
#endif
1043
 
    man_globals->compress = TRUE;
1044
 
    man_globals->deletetempfile = TRUE;
1045
 
    snprintf(man_globals->save_file, sizeof(man_globals->save_file),
1046
 
             "%s/%s%s.%s/%s", path, CAT, section + len_cat, 
1047
 
             COMPRESSION_EXTENSION, page);
1048
 
    return(TRUE);
1049
 
  }
1050
 
  return(FALSE);
 
974
    snprintf(input, sizeof(input), "%s/%s%s.%s/%s", path,
 
975
             MAN, section + len_man, COMPRESSION_EXTENSION, page);
 
976
    if (UncompressNamed(man_globals, input, filename, file)) {
 
977
        man_globals->compress = TRUE;
 
978
        man_globals->deletetempfile = TRUE;
 
979
        snprintf(man_globals->save_file, sizeof(man_globals->save_file),
 
980
                 "%s/%s%s.%s/%s", path, CAT, section + len_cat,
 
981
                 COMPRESSION_EXTENSION, page);
 
982
        return (TRUE);
 
983
    }
 
984
    return (FALSE);
1051
985
}
1052
986
 
1053
987
/*      Function Name: AddCursor
1060
994
void
1061
995
AddCursor(Widget w, Cursor cursor)
1062
996
{
1063
 
  XColor colors[2];
1064
 
  Arg args[10];
1065
 
  Cardinal num_args = 0;
1066
 
  Colormap c_map;
1067
 
 
1068
 
  if (!XtIsRealized(w)) {
1069
 
    PopupWarning(NULL, "Widget is not realized, no cursor added.\n");
1070
 
    return;
1071
 
  }
1072
 
 
1073
 
  XtSetArg( args[num_args], XtNcolormap, &c_map); num_args++;
1074
 
  XtGetValues( w, args, num_args);
1075
 
 
1076
 
  colors[0].pixel = resources.cursors.fg_color;
1077
 
  colors[1].pixel = resources.cursors.bg_color;
1078
 
 
1079
 
  XQueryColors (XtDisplay(w), c_map, colors, 2);
1080
 
  XRecolorCursor(XtDisplay(w), cursor, colors, colors+1);
1081
 
  XDefineCursor(XtDisplay(w),XtWindow(w),cursor);
 
997
    XColor colors[2];
 
998
    Arg args[10];
 
999
    Cardinal num_args = 0;
 
1000
    Colormap c_map;
 
1001
 
 
1002
    if (!XtIsRealized(w)) {
 
1003
        PopupWarning(NULL, "Widget is not realized, no cursor added.\n");
 
1004
        return;
 
1005
    }
 
1006
 
 
1007
    XtSetArg(args[num_args], XtNcolormap, &c_map);
 
1008
    num_args++;
 
1009
    XtGetValues(w, args, num_args);
 
1010
 
 
1011
    colors[0].pixel = resources.cursors.fg_color;
 
1012
    colors[1].pixel = resources.cursors.bg_color;
 
1013
 
 
1014
    XQueryColors(XtDisplay(w), c_map, colors, 2);
 
1015
    XRecolorCursor(XtDisplay(w), cursor, colors, colors + 1);
 
1016
    XDefineCursor(XtDisplay(w), XtWindow(w), cursor);
1082
1017
}
1083
1018
 
1084
1019
/*      Function Name: ChangeLabel
1090
1025
 */
1091
1026
 
1092
1027
void
1093
 
ChangeLabel(Widget w, char * str)
 
1028
ChangeLabel(Widget w, const char *str)
1094
1029
{
1095
 
  Arg arglist[3];               /* An argument list. */
1096
 
 
1097
 
  if (w == NULL) return;
1098
 
 
1099
 
  XtSetArg(arglist[0], XtNlabel, str);
 
1030
    Arg arglist[3];             /* An argument list. */
 
1031
 
 
1032
    if (w == NULL)
 
1033
        return;
 
1034
 
 
1035
    XtSetArg(arglist[0], XtNlabel, str);
1100
1036
 
1101
1037
/* shouldn't really have to do this. */
1102
 
  XtSetArg(arglist[1], XtNwidth, 0);
1103
 
  XtSetArg(arglist[2], XtNheight, 0);
 
1038
    XtSetArg(arglist[1], XtNwidth, 0);
 
1039
    XtSetArg(arglist[2], XtNheight, 0);
1104
1040
 
1105
 
  XtSetValues(w, arglist, (Cardinal) 1);
 
1041
    XtSetValues(w, arglist, (Cardinal) 1);
1106
1042
}
1107
1043
 
1108
1044
/*
1113
1049
/*      Function Name: PositionCenter
1114
1050
 *      Description: This function positions the given widgets center
1115
1051
 *                   in the following location.
1116
 
 *      Arguments: widget - the widget widget to postion
 
1052
 *      Arguments: widget - the widget widget to position
1117
1053
 *                 x,y - The location for the center of the widget
1118
1054
 *                 above - number of pixels above center to locate this widget
1119
1055
 *                 left - number of pixels left of center to locate this widget
1125
1061
 */
1126
1062
 
1127
1063
void
1128
 
PositionCenter(Widget widget, int x, int y, int above, int left, int v_space, int h_space)
 
1064
PositionCenter(Widget widget, int x, int y, int above, int left, int v_space,
 
1065
               int h_space)
1129
1066
{
1130
 
  Arg wargs[2];
1131
 
  int x_temp,y_temp;            /* location of the new window. */
1132
 
  int parent_height,parent_width; /* Height and width of the parent widget or
1133
 
                                   the root window if it has no parent. */
1134
 
 
1135
 
  x_temp = x - left - Width(widget) / 2 + BorderWidth(widget);
1136
 
  y_temp = y - above -  Height(widget) / 2 + BorderWidth(widget);
1137
 
 
1138
 
  parent_height = HeightOfScreen(XtScreen(widget));
1139
 
  parent_width = WidthOfScreen(XtScreen(widget));
 
1067
    Arg wargs[2];
 
1068
    int x_temp, y_temp;         /* location of the new window. */
 
1069
    int parent_height, parent_width;    /* Height and width of the parent widget or
 
1070
                                           the root window if it has no parent. */
 
1071
 
 
1072
    x_temp = x - left - Width(widget) / 2 + BorderWidth(widget);
 
1073
    y_temp = y - above - Height(widget) / 2 + BorderWidth(widget);
 
1074
 
 
1075
    parent_height = HeightOfScreen(XtScreen(widget));
 
1076
    parent_width = WidthOfScreen(XtScreen(widget));
1140
1077
 
1141
1078
/*
1142
1079
 * Check to make sure that all edges are within the viewable part of the
1143
1080
 * root window, and if not then force them to be.
1144
1081
 */
1145
1082
 
1146
 
  if (x_temp < h_space)
1147
 
    x_temp = v_space;
1148
 
  if (y_temp < v_space)
1149
 
    (y_temp = 2);
1150
 
 
1151
 
  if ( y_temp + Height(widget) + v_space > parent_height )
1152
 
      y_temp = parent_height - Height(widget) - v_space;
1153
 
 
1154
 
  if ( x_temp + Width(widget) + h_space > parent_width )
1155
 
      x_temp = parent_width - Width(widget) - h_space;
1156
 
 
1157
 
  XtSetArg(wargs[0], XtNx, x_temp);
1158
 
  XtSetArg(wargs[1], XtNy, y_temp);
1159
 
  XtSetValues(widget, wargs, 2);
 
1083
    if (x_temp < h_space)
 
1084
        x_temp = v_space;
 
1085
    if (y_temp < v_space)
 
1086
        (y_temp = 2);
 
1087
 
 
1088
    if (y_temp + Height(widget) + v_space > parent_height)
 
1089
        y_temp = parent_height - Height(widget) - v_space;
 
1090
 
 
1091
    if (x_temp + Width(widget) + h_space > parent_width)
 
1092
        x_temp = parent_width - Width(widget) - h_space;
 
1093
 
 
1094
    XtSetArg(wargs[0], XtNx, x_temp);
 
1095
    XtSetArg(wargs[1], XtNy, y_temp);
 
1096
    XtSetValues(widget, wargs, 2);
1160
1097
}
1161
1098
 
1162
1099
/*      Function Name: ParseEntry(entry, path, sect, page)
1169
1106
 */
1170
1107
 
1171
1108
void
1172
 
ParseEntry(char *entry, char *path, char *sect, char *page)
 
1109
ParseEntry(const char *entry, char *path, char *sect, char *page)
1173
1110
{
1174
 
  char *c, temp[BUFSIZ];
1175
 
 
1176
 
  strcpy(temp, entry);
1177
 
 
1178
 
  c = rindex(temp, '/');
1179
 
  if (c == NULL)
1180
 
    PrintError("index failure in ParseEntry.");
1181
 
  *c++ = '\0';
1182
 
  if (page != NULL)
1183
 
    strcpy(page, c);
1184
 
 
1185
 
  c = rindex(temp, '/');
1186
 
  if (c == NULL)
1187
 
    PrintError("index failure in ParseEntry.");
1188
 
  *c++ = '\0';
 
1111
    char *c, temp[BUFSIZ];
 
1112
 
 
1113
    strcpy(temp, entry);
 
1114
 
 
1115
    c = strrchr(temp, '/');
 
1116
    if (c == NULL)
 
1117
        PrintError("Failed to find / in ParseEntry.");
 
1118
    *c++ = '\0';
 
1119
    if (page != NULL)
 
1120
        strcpy(page, c);
 
1121
 
 
1122
    c = strrchr(temp, '/');
 
1123
    if (c == NULL)
 
1124
        PrintError("Failed to find / in ParseEntry.");
 
1125
    *c++ = '\0';
1189
1126
#if defined(SFORMAT) && defined(SMAN)
1190
 
  /* sgmltoroff sometimes puts an extra ./ in the path to .so entries */
1191
 
  if (strcmp(c, ".") == 0) {
1192
 
      c = rindex(temp, '/');
1193
 
      if (c == NULL)
1194
 
          PrintError("index failure in ParseEntry.");
1195
 
      *c++ = '\0';
1196
 
  }
1197
 
#endif      
 
1127
    /* sgmltoroff sometimes puts an extra ./ in the path to .so entries */
 
1128
    if (strcmp(c, ".") == 0) {
 
1129
        c = strrchr(temp, '/');
 
1130
        if (c == NULL)
 
1131
            PrintError("Failed to find / in ParseEntry.");
 
1132
        *c++ = '\0';
 
1133
    }
 
1134
#endif
1198
1135
#if defined(__OpenBSD__) || defined(__NetBSD__)
1199
 
  /* Skip machine subdirectory if present */
1200
 
  if (strcmp(c, MACHINE) == 0) {
1201
 
      c = rindex(temp, '/');
1202
 
      if (c == NULL)
1203
 
          PrintError("index failure in ParseEntry.");
1204
 
      *c++ = '\0';
1205
 
  }
 
1136
    /* Skip machine subdirectory if present */
 
1137
    if (strcmp(c, MACHINE) == 0) {
 
1138
        c = strrchr(temp, '/');
 
1139
        if (c == NULL)
 
1140
            PrintError("Failed to find / in ParseEntry.");
 
1141
        *c++ = '\0';
 
1142
    }
1206
1143
#endif
1207
 
  if (sect != NULL)
1208
 
    strcpy(sect, c);
 
1144
    if (sect != NULL)
 
1145
        strcpy(sect, c);
1209
1146
 
1210
 
  if (path != NULL)
1211
 
    strcpy(path, temp);
 
1147
    if (path != NULL)
 
1148
        strcpy(path, temp);
1212
1149
}
1213
1150
 
1214
1151
/*      Function Name: GetGlobals
1215
 
 *      Description: Gets the psuedo globals associated with the
 
1152
 *      Description: Gets the pseudo globals associated with the
1216
1153
 *                   manpage associated with this widget.
1217
1154
 *      Arguments: w - a widget in the manpage.
1218
 
 *      Returns: the psuedo globals.
 
1155
 *      Returns: the pseudo globals.
1219
1156
 *      Notes: initial_widget is a globals variable.
1220
1157
 *             manglobals_context is a global variable.
1221
1158
 */
1223
1160
ManpageGlobals *
1224
1161
GetGlobals(Widget w)
1225
1162
{
1226
 
  Widget temp;
1227
 
  caddr_t data;
1228
 
 
1229
 
  while ( (temp = XtParent(w)) != initial_widget && (temp != NULL))
1230
 
    w = temp;
1231
 
 
1232
 
  if (temp == NULL)
1233
 
    XtAppError(XtWidgetToApplicationContext(w),
1234
 
               "Xman: Could not locate widget in tree, exiting");
1235
 
 
1236
 
  if (XFindContext(XtDisplay(w), XtWindow(w),
1237
 
                   manglobals_context, &data) != XCSUCCESS)
1238
 
    XtAppError(XtWidgetToApplicationContext(w),
1239
 
               "Xman: Could not find global data, exiting");
1240
 
 
1241
 
  return( (ManpageGlobals *) data);
 
1163
    Widget temp;
 
1164
    caddr_t data;
 
1165
 
 
1166
    while ((temp = XtParent(w)) != initial_widget && (temp != NULL))
 
1167
        w = temp;
 
1168
 
 
1169
    if (temp == NULL)
 
1170
        XtAppError(XtWidgetToApplicationContext(w),
 
1171
                   "Xman: Could not locate widget in tree, exiting");
 
1172
 
 
1173
    if (XFindContext(XtDisplay(w), XtWindow(w),
 
1174
                     manglobals_context, &data) != XCSUCCESS)
 
1175
        XtAppError(XtWidgetToApplicationContext(w),
 
1176
                   "Xman: Could not find global data, exiting");
 
1177
 
 
1178
    return ((ManpageGlobals *) data);
1242
1179
}
1243
1180
 
1244
1181
/*      Function Name: SaveGlobals
1245
 
 *      Description: Saves the psuedo globals on the widget passed
 
1182
 *      Description: Saves the pseudo globals on the widget passed
1246
1183
 *                   to this function, although GetGlobals assumes that
1247
1184
 *                   the data is associated with the popup child of topBox.
1248
1185
 *      Arguments: w - the widget to associate the data with.
1255
1192
void
1256
1193
SaveGlobals(Widget w, ManpageGlobals * globals)
1257
1194
{
1258
 
  if (XSaveContext(XtDisplay(w), XtWindow(w), manglobals_context,
1259
 
                   (caddr_t) globals) != XCSUCCESS)
1260
 
    XtAppError(XtWidgetToApplicationContext(w),
1261
 
               "Xman: Could not save global data, are you out of memory?");
 
1195
    if (XSaveContext(XtDisplay(w), XtWindow(w), manglobals_context,
 
1196
                     (caddr_t) globals) != XCSUCCESS)
 
1197
        XtAppError(XtWidgetToApplicationContext(w),
 
1198
                   "Xman: Could not save global data, are you out of memory?");
1262
1199
}
1263
1200
 
1264
1201
/*      Function Name: RemoveGlobals
1265
 
 *      Description: Removes the psuedo globals from the widget passed
 
1202
 *      Description: Removes the pseudo globals from the widget passed
1266
1203
 *                   to this function.
1267
1204
 *      Arguments: w - the widget to remove the data from.
1268
1205
 *      Returns: none.
1273
1210
void
1274
1211
RemoveGlobals(Widget w)
1275
1212
{
1276
 
  if (XDeleteContext(XtDisplay(w), XtWindow(w),
1277
 
                     manglobals_context) != XCSUCCESS)
1278
 
    XtAppError(XtWidgetToApplicationContext(w),
1279
 
               "Xman: Could not remove global data?");
 
1213
    if (XDeleteContext(XtDisplay(w), XtWindow(w),
 
1214
                       manglobals_context) != XCSUCCESS)
 
1215
        XtAppError(XtWidgetToApplicationContext(w),
 
1216
                   "Xman: Could not remove global data?");
1280
1217
}