File: callbacks.c

package info (click to toggle)
xless 1.7-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ansic: 3,135; makefile: 38
file content (554 lines) | stat: -rw-r--r-- 13,113 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * Copyright (C) 1994 by Dave Glowacki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * to rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * DAVE GLOWACKI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * $Header: /usr/sww/share/src/X11R6/local/applications/xless-1.7/RCS/callbacks.c,v 1.37 1994/07/29 02:28:14 dglo Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>

#include <X11/X.h>
#include <X11/Xos.h>

#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>

#include <X11/Xaw/AsciiText.h>

#ifdef DEBIAN
#define __DO_NOT_DEFINE_COMPILE
#include <regex.h>
#else
#include "regexp/regexp.h"
#endif
#include "xless.h"

extern char *getenv __P((const char *));
extern int system __P((const char *));

static void getReadOnlySource __P((Widget, const char *));
static int find __P((const char *, WindowInfo *, XawTextPosition));
static void doSearch __P((WindowInfo *));
static void popdownAndSearch __P((Widget, XtPointer, XtPointer));
static void popdownAndChange __P((Widget, XtPointer, XtPointer));
static void popdownAndCreate __P((Widget, XtPointer, XtPointer));

static void
getReadOnlySource(w, string)
Widget w;
const char *string;
{
  Widget source;

  /* free old source */
  source = XawTextGetSource(w);
  XtDestroyWidget(source);

  /* Create a new source for the text widget, and put in the new string */
  source = XtVaCreateWidget("readText", asciiSrcObjectClass, w,
			    XtNeditType, XawtextRead,
			    XtNstring, string,
			    XtNuseStringInPlace, True,
			    NULL);

  XawTextSetSource(w, source, (XawTextPosition) 0);
}

/*
 * Button callback functions.
 */

void
Quit(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  QuitFunction();
}

void
Cancel(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  XtPopdown((Widget)closure);
}

void
CallEditor(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;
  const char *editor;
  int cmdlen = 0;
  char *cmd;

  /* figure out which editor they want */
  if ((editor = getenv("VISUAL")) == NULL &&
      (editor = getenv("EDITOR")) == NULL)
    editor = (resources.editor ? resources.editor : DEFEDITOR);

  /* get a buffer long enough for the entire command */
  if (!resources.editorDoesWindows)
    cmdlen = 9;			/* "xterm -e " */
  cmdlen += strlen(editor) + 1 + strlen(wi->file) + 2;
  cmd = (char *)XtMalloc((Cardinal )cmdlen);

  /* don't start from an xterm if the editor has its own window */
  if (resources.editorDoesWindows)
    strcpy(cmd, editor);
  else {
    strcpy(cmd, "xterm -e ");
    strcat(cmd, editor);
  }
  strcat(cmd, " ");
  strcat(cmd, wi->file);
  strcat(cmd, "&");
  system(cmd);
  XtFree(cmd);
}

void
Reload(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;
  int fd;

  fd = open(wi->file, O_RDONLY);
  if (fd == -1)
    CouldntOpen(wi->base, wi->file);
  else {
    XtFree((char *)wi->memory);
    InitData(fd, wi);
    getReadOnlySource(wi->text, wi->memory);
  }
}

#ifdef DEBIAN
static int
find(text, wi, offset)
const char *text;
WindowInfo *wi;
XawTextPosition offset;
{
  static Widget badPattern = NULL;
  char *str = wi->searchBuf;
  XawTextPosition beg, end;
  regex_t pat;
  regmatch_t pmatch;
  int rtnval;

  /* try to match the pattern */
  rtnval = 0;
  if (regcomp(&pat, str, REG_EXTENDED) != 0) {
    if (!badPattern)
      badPattern = MessageBox(wi->base, "Bad pattern ...", "OK", 0, 0);
    if (badPattern)
      SetPopup(wi->base, badPattern);
  } else if ((regexec(&pat, text, 1, &pmatch, 0) == 0) && (pmatch.rm_so != -1)) {
    beg = offset + pmatch.rm_so;
    end = offset + pmatch.rm_eo;
    XawTextSetInsertionPoint(wi->text, end);
    XawTextSetSelection(wi->text, beg, end);
  } else
    rtnval = -1;

  return(rtnval);
}
#else
static int
find(text, wi, offset)
const char *text;
WindowInfo *wi;
XawTextPosition offset;
{
  const char *special = "\\|*+?.^$[]()";
  static Widget badPattern = NULL;
  char *str = wi->searchBuf;
  XawTextPosition beg, end;
  regexp *pat;
  char *s;
  Boolean insensitive, allocated;
  int len;
  unsigned slen, nSpecial, nAlpha;
  char *newstr;
  int rtnval;

  /* is this a case-insensitive search? */
  insensitive = (wi->flag & XLessSearchInsensitive) == XLessSearchInsensitive;
  allocated = 0;

  /* if we need to escape special characters... */
  if ((wi->flag & XLessSearchRegExpr) != XLessSearchRegExpr) {

    /* count the special characters */
    nSpecial = nAlpha = 0;
    for (s = str; *s; s++) {
      if (strchr(special, *s) != NULL)
	nSpecial++;
      else if (insensitive && isalpha(*s))
	nAlpha++;
    }

    /* if we need to escape something... */
    if (nSpecial > 0 || (insensitive && nAlpha > 0)) {

      /* figure out how much space we'll need */
      len = strlen(str);
      if (insensitive)
	slen = nAlpha * 4 + nSpecial * 2 + (len - (nSpecial + nAlpha));
      else
	slen = len + nSpecial;

      /* get space for the new string */
      newstr = (char *)XtMalloc(slen + 1);
      if (newstr == NULL) {
	fprintf(stderr, "Out of space in search routine!\n");
	exit(1);
      }

      /* build the special string */
      for (s = newstr; *str; str++) {
	if (strchr(special, *str) != NULL) {
	  *s++ = '\\';
	  *s++ = *str;
	} else if (insensitive && isalpha(*str)) {
	  *s++ = '[';
	  *s++ = *str;
	  if (isupper(*str))
	    *s++ = tolower(*str);
	  else
	    *s++ = toupper(*str);
	  *s++ = ']';
	} else
	  *s++ = *str; 
      }
      *s = 0;

      /* allocated string is the search pattern now */
      allocated = 1; 
      str = newstr;
    }
  }

  /* try to match the pattern */
  rtnval = 0;
  pat = regcomp(str);
  if (pat == NULL) {
    if (!badPattern)
      badPattern = MessageBox(wi->base, "Bad pattern ...", "OK", 0, 0);
    if (badPattern)
      SetPopup(wi->base, badPattern);
  } else if (regexec(pat, text)) {
    beg = offset + (pat->startp[0] - text);
    end = beg + (pat->endp[0] - pat->startp[0]);
    XawTextSetInsertionPoint(wi->text, end);
    XawTextSetSelection(wi->text, beg, end);
  } else
    rtnval = -1;

  /* clean up allocated memory */
  if (allocated)
    XtFree(str);
  if (pat)
    XtFree((char *)pat);

  return(rtnval);
}
#endif

static void
doSearch(wi)
WindowInfo *wi;
{
  XawTextPosition offset;
  int len;
  const char *top;
  static Widget notFound = NULL;

  len = strlen(wi->searchBuf);
  offset = XawTextGetInsertionPoint(wi->text);
  top = wi->memory + offset;
  if (len <= 0 || find(top, wi, offset)) {
    if (!notFound)
      notFound = MessageBox(wi->base, "String not found ...", "OK", 0, 0);
    if (notFound)
      SetPopup(wi->base, notFound);
  }
}

void
SearchNext(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;
  static Widget noString = NULL;

  if (wi->searchBuf == NULL) {
    if (!noString)
      noString = MessageBox(wi->base, "No search string specified ...",
			    "OK", 0, 0);
    if (noString)
      SetPopup(wi->base, noString);
  } else
    doSearch(wi);
}

static void
popdownAndSearch(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;

  XtPopdown((Widget)wi->searchPopup);
  doSearch(wi);
}

void
Search(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;

  if (!wi->searchBuf) {
    wi->searchBuf = (char *)XtMalloc(XLESS_MAX_INPUT);
    wi->searchBuf[0] = 0;
  }

  if (!wi->searchPopup)
    wi->searchPopup = SearchBox(wi->base, popdownAndSearch, (XtPointer )wi,
				"Search for:", "Search", wi->searchBuf);

  /* popup search dialog box */
  SetPopup(wi->base, wi->searchPopup);
}

static void
popdownAndChange(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;
  int fd;
  const char *filename;
  XLessFlag flag = XLessClearFlag;

  XtPopdown((Widget)wi->changeFilePopup);

  /* make sure there's a filename there */
  filename = wi->changeFileBuf;
  if (*filename == 0)
    return;

#ifdef TILDE_EXPANSION
    /* see if we need to do tilde expansion */
    if (filename && *filename == '~') {
      filename = TildeExpand(filename);
      if (*filename != '~')
	flag |= XLessFreeFilename;
    }
#endif /* TILDE_EXPANSION */

  fd = open(filename, O_RDONLY);
  if (fd == -1)
    CouldntOpen(wi->base, filename);
  else {

    /* read in new file */
    XtFree((char *)wi->memory);
    InitData(fd, wi);
    if (wi->flag & XLessFreeFilename) {
      XtFree((char *)wi->file);
      wi->flag &= ~XLessFreeFilename;
    }
    getReadOnlySource(wi->text, wi->memory);
    wi->file = filename;
    wi->flag |= flag;

    /* sensitize buttons if previous file was STDIN */
    if (wi->editorButton) {
      XtVaSetValues(wi->editorButton, XtNsensitive, True, NULL);
      wi->editorButton = 0;
    }
    if (wi->reloadButton) {
      XtVaSetValues(wi->reloadButton, XtNsensitive, True, NULL);
      wi->editorButton = 0;
    }

    /* set title & icon name */
    SetXNames(wi->base, filename);
  }
}

void
ChangeFile(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;

  if (!wi->changeFileBuf) {
    wi->changeFileBuf = (char *)XtMalloc(XLESS_MAX_INPUT);
    wi->changeFileBuf[0] = 0;
  }

  if (!wi->changeFilePopup)
    wi->changeFilePopup = DialogBox(wi->base, popdownAndChange, (XtPointer)wi,
				    "Enter filename:", "Ok",
				    wi->changeFileBuf);

  SetPopup(wi->base, wi->changeFilePopup);	/* Change file dialog box */
}

static void
popdownAndCreate(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;

  XtPopdown((Widget)wi->newWindowPopup);
  CreateWindow(wi->base, wi->newWindowBuf);
}

void
NewWindow(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;

  if (!wi->newWindowBuf) {
    wi->newWindowBuf = (char *)XtMalloc(XLESS_MAX_INPUT);
    wi->newWindowBuf[0] = 0;
  }

  if (!wi->newWindowPopup)
    wi->newWindowPopup = DialogBox(wi->base, popdownAndCreate, (XtPointer)wi,
			"Enter filename:", "Ok", wi->newWindowBuf);
  SetPopup(wi->base, wi->newWindowPopup);	/* New window dialog box */
}

void
CloseWindow(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *sd = (WindowInfo *)closure;

  DestroyWindowInfo(sd);
}

void
Print(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  WindowInfo *wi = (WindowInfo *)closure;
  FILE *file;
  const char *printfile;
  char *cmd;
#ifdef WIMPY_STDIO
  char *line, *l;
#endif

  if (wi->file == NULL) {

    /* create a temporary file */
    printfile = tmpnam(0);
    file = fopen(printfile, "w");

#ifdef WIMPY_STDIO
    /* write stdin (line by line) to temporary file */
    line = l = wi->memory;
    while (line && *line) {
      if (l = strchr(line, '\n'))
	*l = 0;
      fprintf(file, "%s\n", line);
      if (l)
	*l = '\n';
      line = l+1;
    }
#else /* !WIMPY_STDIO */
    /* write stdin (in one manly hunk) to temp file */
    fputs(wi->memory, file);
#endif /* WIMPY_STDIO */
    fclose(file);
  } else
    printfile = wi->file;

  /* create the print command string */
  cmd = (char *)XtMalloc((Cardinal )(strlen(resources.printCmd) + 1 +
				     strlen(printfile) + 1));
  strcpy(cmd, resources.printCmd);
  strcat(cmd, " ");
  strcat(cmd, printfile);
  system(cmd);
  XtFree(cmd);

  /* unlink the file if we created it */
  if (wi->file == NULL)
    unlink(printfile);
}

void
Background(widget, closure, callData)
Widget widget;
XtPointer closure;
XtPointer callData;
{
  static Widget forkError = NULL;
  WindowInfo *wi = (WindowInfo *)closure;

  switch (fork()) {
  case 0:
    break;
  case -1:
    if (!forkError)
      forkError = MessageBox(wi->base, "Error while backgrounding ...",
			    "OK", 0, 0);
    if (forkError)
      SetPopup(wi->base, forkError);
    break;
  default:
    exit(0);
  }
}