about summary refs log tree commit diff stats
path: root/src/HTAlert.c
blob: 93fb0fed1948b23e2ab23f5b08f8ad18e1a61fcc (plain) (blame)
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
555
556
557
558
/*	Displaying messages and getting input for Lynx Browser
**	==========================================================
**
**	REPLACE THIS MODULE with a GUI version in a GUI environment!
**
** History:
**	   Jun 92 Created May 1992 By C.T. Barker
**	   Feb 93 Simplified, portablised TBL
**
*/

#include "HTUtils.h"
#include "tcp.h"
#include "HTAlert.h"
#include "LYGlobalDefs.h"
#include "LYCurses.h"
#include "LYStrings.h"
#include "LYUtils.h"
#include "LYSignal.h"
#include "GridText.h"
#include "LYCookie.h"

#include "LYLeaks.h"

#define FREE(x) if (x) {free(x); x = NULL;}


/*	Issue a message about a problem.		HTAlert()
**	--------------------------------
*/
PUBLIC void HTAlert ARGS1(
	CONST char *,	Msg)
{
    if (TRACE) {
        fprintf(stderr, "\nAlert!: %s", (char *)Msg);
	fflush(stderr);
        _user_message("Alert!: %s", (char *)Msg);
        fprintf(stderr, "\n\n");
	fflush(stderr);
    } else
        _user_message("Alert!: %s", (char *)Msg);

    sleep(AlertSecs);
}

/*	Issue a progress message.			HTProgress()
**	-------------------------
*/
PUBLIC void HTProgress ARGS1(
	CONST char *,	Msg)
{
    if (TRACE)
        fprintf(stderr, "%s\n", (char *)Msg);
    else
        statusline((char *)Msg);
}

/*	Seek confirmation.				HTConfirm()
**	------------------
*/
PUBLIC BOOL HTConfirm ARGS1(CONST char *, Msg)
{
    if (dump_output_immediately) { /* Non-interactive, can't respond */
	return(NO);
    } else {
	int c;
#ifdef VMS
	extern BOOLEAN HadVMSInterrupt;
#endif /* VMS */
	
	_user_message("%s (y/n) ", (char *)Msg);
	
	while (1) {
	    c = LYgetch();
#ifdef VMS
	    if (HadVMSInterrupt) {
		HadVMSInterrupt = FALSE;
		c = 'N';
	    }
#endif /* VMS */
	    if (TOUPPER(c) == 'Y')
		return(YES);
	    if (TOUPPER(c) == 'N' || c == 7 || c == 3) /* ^G or ^C cancels */
		return(NO);
	}
    }
}

/*	Prompt for answer and get text back.		HTPrompt()
**	------------------------------------
*/
PUBLIC char * HTPrompt ARGS2(
	CONST char *,	Msg,
	CONST char *,	deflt)
{
    char * rep = NULL;
    char Tmp[200];

    Tmp[0] = '\0';
    Tmp[199] = '\0';

    _statusline((char *)Msg);
    if (deflt) 
        strncpy(Tmp, deflt, 199);

    if (!dump_output_immediately)
        LYgetstr(Tmp, VISIBLE, sizeof(Tmp), NORECALL);

    StrAllocCopy(rep, Tmp);

    return rep;
}

/*
**	Prompt for password without echoing the reply.	HTPromptPassword()
**	----------------------------------------------
*/
PUBLIC char * HTPromptPassword ARGS1(
	CONST char *,	Msg)
{
    char *result = NULL;
    char pw[120];

    pw[0] = '\0';

    if (!dump_output_immediately) {
        _statusline(Msg ? (char *)Msg : PASSWORD_PROMPT);
        LYgetstr(pw, HIDDEN, sizeof(pw), NORECALL); /* hidden */
        StrAllocCopy(result, pw);
    } else {
        printf("\n%s\n", PASSWORD_REQUIRED);
	StrAllocCopy(result, "");
    }
    return result;
}

/*     	Prompt both username and password.       HTPromptUsernameAndPassword()
**      ----------------------------------
**
**  On entry,
**      Msg             is the prompting message.
**      *username and
**      *password       are char pointers which contain default
**			or zero-length strings; they are changed
**                      to point to result strings.
**	IsProxy		should be TRUE if this is for
**			proxy authentication.
**
**                      If *username is not NULL, it is taken
**                      to point to a default value.
**                      Initial value of *password is
**                      completely discarded.
**
**  On exit,
**      *username and *password point to newly allocated
**      strings -- original strings pointed to by them
**      are NOT freed.
**
*/
PUBLIC void HTPromptUsernameAndPassword ARGS4(
	CONST char *,	Msg,
	char **,	username,
	char **,	password,
	BOOL,		IsProxy)
{
    if ((IsProxy == FALSE &&
	 authentication_info[0] && authentication_info[1]) ||
	(IsProxy == TRUE &&
	 proxyauth_info[0] && proxyauth_info[1])) {
	/* 
	**  The -auth or -pauth parameter gave us both the username
	**  and password to use for the first realm or proxy server,
	**  respectively, so just use them without any prompting. - FM
	*/
 	StrAllocCopy(*username, (IsProxy ?
		       proxyauth_info[0] : authentication_info[0]));
	if (IsProxy) {
	    FREE(proxyauth_info[0]);
	} else {
	    FREE(authentication_info[0]);
	}
	StrAllocCopy(*password, (IsProxy ?
		       proxyauth_info[1] : authentication_info[1]));
	if (IsProxy) {
	    FREE(proxyauth_info[1]);
	} else {
	    FREE(authentication_info[1]);
	}
    } else if (dump_output_immediately) {
        /*
	 *  We are not interactive and don't have both the
	 *  username and password from the command line,
	 *  but might have one or the other. - FM
	 */
        if ((IsProxy == FALSE && authentication_info[0]) ||
	    (IsProxy == TRUE && proxyauth_info[0])) {
	    /*
	    **  Use the command line username. - FM
	    */
	    StrAllocCopy(*username, (IsProxy ?
			   proxyauth_info[0] : authentication_info[0]));
	    if (IsProxy) {
		FREE(proxyauth_info[0]);
	    } else {
		FREE(authentication_info[0]);
	    }
	} else {
	    /*
	    **  Default to "WWWuser". - FM
	    */
            StrAllocCopy(*username, "WWWuser");
	}
        if ((IsProxy == FALSE && authentication_info[1]) ||
	    (IsProxy == TRUE && proxyauth_info[1])) {
	    /*
	    **  Use the command line password. - FM
	    */
	    StrAllocCopy(*password, (IsProxy ?
			   proxyauth_info[1] : authentication_info[1]));
	    if (IsProxy) {
		FREE(proxyauth_info[1]);
	    } else {
		FREE(authentication_info[1]);
	    }
	} else {
	    /*
	    **  Default to a zero-length string. - FM
	    */
	    StrAllocCopy(*password, "");
	}
	printf("\n%s\n", USERNAME_PASSWORD_REQUIRED);

    } else {
        /*
	 *  We are interactive and don't have both the
	 *  username and password from the command line,
	 *  but might have one or the other. - FM
	 */
        if ((IsProxy == FALSE && authentication_info[0]) ||
	    (IsProxy == TRUE && proxyauth_info[0])) {
	    /*
	    **  Offer the command line username in the
	    **  prompt for the first realm. - FM
	    */
	    StrAllocCopy(*username, (IsProxy ?
			   proxyauth_info[0] : authentication_info[0]));
	    if (IsProxy) {
		FREE(proxyauth_info[0]);
	    } else {
		FREE(authentication_info[0]);
	    }
	}
	/*
	 *  Prompt for confirmation or entry of the username. - FM
	 */
	if (Msg != NULL) {
	    *username = HTPrompt(Msg, *username);
	} else {
	    *username = HTPrompt(USERNAME_PROMPT, *username);
	}
        if ((IsProxy == FALSE && authentication_info[1]) ||
	    (IsProxy == TRUE && proxyauth_info[1])) {
	    /*
	    **  Use the command line password for the first realm. - FM
	    */
	    StrAllocCopy(*password, (IsProxy ?
			   proxyauth_info[1] : authentication_info[1]));
	    if (IsProxy) {
		FREE(proxyauth_info[1]);
	    } else {
		FREE(authentication_info[1]);
	    }
	} else if (*username != NULL && *username[0] != '\0') {
	    /*
	    **  We have a non-zero length username,
	    **  so prompt for the password. - FM
	    */
	    *password = HTPromptPassword(PASSWORD_PROMPT);
	} else {
	    /*
	    **  Return a zero-length password. - FM
	    */
	    StrAllocCopy(*password, "");
	}
    }
}

/*	Confirm a cookie operation.			HTConfirmCookie()
**	---------------------------
**
**  On entry,
**	server			is the server sending the Set-Cookie.
**	domain			is the domain of the cookie.
**	path			is the path of the cookie.
**	name			is the name of the cookie.
**	value			is the value of the cookie.
**
**  On exit,
**	Returns FALSE on cancel,
**		TRUE if the cookie should be set.
*/
PUBLIC BOOL HTConfirmCookie ARGS6(
	void *,		dp,
	CONST char *,	server,
	CONST char *,	domain,
	CONST char *,	path,
	CONST char *,	name,
	CONST char *,	value)
{
    char message[256];
    domain_entry *de;
    int ch, namelen, valuelen, space_free;

#ifdef VMS
    extern BOOLEAN HadVMSInterrupt;
#endif /* VMS */

    if ((de = (domain_entry *)dp) == NULL)
        return FALSE;
  
    /*
    **  If the user has specified a constant action, don't prompt at all.
    */
    if (de->bv == ACCEPT_ALWAYS)
        return TRUE;
    if (de->bv == REJECT_ALWAYS)
        return FALSE;

    if (dump_output_immediately) {
        /*
	**  Non-interactive, can't respond.  Use the LYSetCookies value
	*   based on its compilation or configuration setting, or on the
	**  command line toggle. - FM
	*/
        return LYSetCookies;
    }

    /*
    **  Figure out how much of the cookie we can show.
    **  The '37' is the length of ADVANCED_COOKIE_CONFIRMATION,
    **  minus the length of the %s directives (10 chars)
    */
    if (de != NULL) {
        if (de->bv == ACCEPT_ALWAYS) 
	    return TRUE;
	if (de->bv == REJECT_ALWAYS) 
	    return FALSE;
    }
    space_free = (((LYcols - 1) - 37) - strlen(server));
    if (space_free < 0)
        space_free = 0;
    namelen = strlen(name);
    valuelen = strlen(value);
    if ((namelen + valuelen) > space_free) {
        /*
	**  Argh... there isn't enough space on our single line for
	**  the whole cookie.  Reduce them both by a percentage.
	**  This should be smarter.
	*/
        float percentage;
        percentage = (float)space_free/(float)(namelen + valuelen);
        namelen = (int)(percentage*(float)namelen);
        valuelen = (int)(percentage*(float)valuelen);
    }
    sprintf(message, ADVANCED_COOKIE_CONFIRMATION,
    	    server, namelen, name, valuelen, value);
    _statusline(message);
    while (1) {
	ch = LYgetch();
#ifdef VMS
	if (HadVMSInterrupt) {
	    HadVMSInterrupt = FALSE;
	    ch = 'N';
	}
#endif /* VMS */
	switch(TOUPPER(ch)) {
	    case 'A':
	        /*
		**  Set to accept all cookies for this domain.
		*/
		de->bv = ACCEPT_ALWAYS;
		_user_message(ALWAYS_ALLOWING_COOKIES, de->domain);
		sleep(MessageSecs);
		return TRUE;

	    case 'N':
	    case 7:	/* Ctrl-G */
	    case 3:	/* Ctrl-C */
	        /*
		**  Reject the cookie.
		*/
		_statusline(REJECTING_COOKIE);
		sleep(MessageSecs);
		return FALSE;

    	    case 'V':
	        /*
		**  Set to reject all cookies from this domain.
		*/
		de->bv = REJECT_ALWAYS;
		_user_message(NEVER_ALLOWING_COOKIES, de->domain);
		sleep(MessageSecs);
		return FALSE;

	    case 'Y':
	        /*
		**  Accept the cookie.
		*/
		_statusline(ALLOWING_COOKIE);
		sleep(InfoSecs);
		return TRUE;

	    default:
	        continue;
	}
    }
}

/*      Confirm redirection of POST.		HTConfirmPostRedirect()
**	----------------------------
**
**  On entry,
**      Redirecting_url             is the Location.
**	server_status		    is the server status code.
**
**  On exit,
**      Returns 0 on cancel,
**	  1 for redirect of POST with content,
**	303 for redirect as GET without content
*/
PUBLIC int HTConfirmPostRedirect ARGS2(
	CONST char *,	Redirecting_url,
	int,		server_status)
{
    char *show_POST_url = NULL;
    char StatusInfo[256];
    char url[256];
    int on_screen = 0;	/* 0 - show menu
   			 * 1 - show url
			 * 2 - menu is already on screen */

    if (server_status == 303 ||
        server_status == 302) {
	/*
	 *  HTTP.c should not have called us for either of
	 *  these because we're treating 302 as historical,
	 *  so just return 303. - FM
	 */
	return 303;
    }

    if (dump_output_immediately)
        if (server_status == 301) {
	    /*
	    **  Treat 301 as historical, i.e., like 303 (GET
	    **  without content), when not interactive. - FM
	    */
            return 303;
        } else {
	    /*
	    **  Treat anything else (e.g., 305, 306 or 307) as too
	    **  dangerous to redirect without confirmation, and thus
	    **  cancel when not interactive. - FM
	    */
	    return 0;
	}

    StatusInfo[254] = StatusInfo[255] = '\0';
    url[254] = url[(LYcols < 250 ? LYcols-1 : 255)] = '\0';
    if (user_mode == NOVICE_MODE) {
        on_screen = 2;
        move(LYlines-2, 0);
        sprintf(StatusInfo, SERVER_ASKED_FOR_REDIRECTION, server_status);
	addstr(StatusInfo);
	clrtoeol();
        move(LYlines-1, 0);
	sprintf(url, "URL: %.*s",
		    (LYcols < 250 ? LYcols-6 : 250), Redirecting_url);
        addstr(url);
	clrtoeol();
	if (server_status == 301) {
	    _statusline(PROCEED_GET_CANCEL);
	} else {
	    _statusline(PROCEED_OR_CANCEL);
	}
    } else {
	sprintf(StatusInfo, "%d %.*s",
			    server_status,
			    251,
			    ((server_status == 301) ?
			 ADVANCED_POST_GET_REDIRECT :
			 ADVANCED_POST_REDIRECT));
	StrAllocCopy(show_POST_url, LOCATION_HEADER);
	StrAllocCat(show_POST_url, Redirecting_url);
    }
    while (1) {
	int c;  

	switch (on_screen) {
	    case 0:
		_statusline(StatusInfo);
		break;
	    case 1:
	        _statusline(show_POST_url);
	}
	c = LYgetch();
	switch (TOUPPER(c)) {
	    case 'P':
		/*
		**  Proceed with 301 or 307 redirect of POST
		**  with same method and POST content. - FM
		*/
	        FREE(show_POST_url);
		return 1;	

 	    case 7:
 	    case 'C':
	        /*
		**  Cancel request.
		*/
	        FREE(show_POST_url);
		return 0;

	    case 'U':
	        /*
		**  Show URL for intermediate or advanced mode.
		*/
	        if (user_mode != NOVICE_MODE) {
		    if (on_screen == 1) {
			on_screen = 0;
		    } else {
			on_screen = 1;
		    }
		}
		break;

	    case 'G':
		if (server_status == 301) {
		    /*
		    **  Treat as 303 (GET without content).
		    */
		    FREE(show_POST_url);
		    return 303;
		}
		/* fall through to default */

	    default:
	        /*
		**  Get another character.
		*/
		if (on_screen == 1) {
		    on_screen = 0;
		} else {
		    on_screen = 2;
		}
	}
    }
}
class="w"> close(STDIN_FILENO); while(sel) { resize(sel, True, TopLeft); unmanage(sel); } if(dc.font.set) XFreeFontSet(dpy, dc.font.set); else XFreeFont(dpy, dc.font.xfont); XUngrabKey(dpy, AnyKey, AnyModifier, root); XFreePixmap(dpy, dc.drawable); XFreeGC(dpy, dc.gc); XDestroyWindow(dpy, barwin); XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); XSync(dpy, False); } static void scan() { unsigned int i, num; Window *wins, d1, d2; XWindowAttributes wa; wins = NULL; if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { for(i = 0; i < num; i++) { if(!XGetWindowAttributes(dpy, wins[i], &wa)) continue; if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) continue; if(wa.map_state == IsViewable) manage(wins[i], &wa); } } if(wins) XFree(wins); } static void setup() { int i, j; unsigned int mask; Window w; XModifierKeymap *modmap; XSetWindowAttributes wa; /* init atoms */ wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast); /* init cursors */ cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); modmap = XGetModifierMapping(dpy); for (i = 0; i < 8; i++) { for (j = 0; j < modmap->max_keypermod; j++) { if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) numlockmask = (1 << i); } } XFree(modmap); wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; wa.cursor = cursor[CurNormal]; XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); grabkeys(); initrregs(); for(ntags = 0; tags[ntags]; ntags++); seltag = emallocz(sizeof(Bool) * ntags); seltag[0] = True; /* style */ dc.norm[ColBG] = getcolor(NORMBGCOLOR); dc.norm[ColFG] = getcolor(NORMFGCOLOR); dc.sel[ColBG] = getcolor(SELBGCOLOR); dc.sel[ColFG] = getcolor(SELFGCOLOR); dc.status[ColBG] = getcolor(STATUSBGCOLOR); dc.status[ColFG] = getcolor(STATUSFGCOLOR); setfont(FONT); bmw = textw(FLOATSYMBOL) > textw(TILESYMBOL) ? textw(FLOATSYMBOL) : textw(TILESYMBOL); sx = sy = 0; sw = DisplayWidth(dpy, screen); sh = DisplayHeight(dpy, screen); mw = (sw * MASTERW) / 100; bx = by = 0; bw = sw; dc.h = bh = dc.font.height + 2; wa.override_redirect = 1; wa.background_pixmap = ParentRelative; wa.event_mask = ButtonPressMask | ExposureMask; barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); XDefineCursor(dpy, barwin, cursor[CurNormal]); XMapRaised(dpy, barwin); dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); dc.gc = XCreateGC(dpy, root, 0, 0); XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); strcpy(stext, "dwm-"VERSION); } /* * Startup Error handler to check if another window manager * is already running. */ static int xerrorstart(Display *dsply, XErrorEvent *ee) { otherwm = True; return -1; } /* extern */ int getproto(Window w) { int i, format, protos, status; unsigned long extra, res; Atom *protocols, real; protos = 0; status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False, XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols); if(status != Success || protocols == 0) return protos; for(i = 0; i < res; i++) if(protocols[i] == wmatom[WMDelete]) protos |= PROTODELWIN; free(protocols); return protos; } void sendevent(Window w, Atom a, long value) { XEvent e; e.type = ClientMessage; e.xclient.window = w; e.xclient.message_type = a; e.xclient.format = 32; e.xclient.data.l[0] = value; e.xclient.data.l[1] = CurrentTime; XSendEvent(dpy, w, False, NoEventMask, &e); XSync(dpy, False); } void quit(Arg *arg) { readin = running = False; } /* * There's no way to check accesses to destroyed windows, thus those cases are * ignored (especially on UnmapNotify's). Other types of errors call Xlibs * default error handler, which calls exit(). */ int xerror(Display *dpy, XErrorEvent *ee) { if(ee->error_code == BadWindow || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)) return 0; fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code); return xerrorxlib(dpy, ee); /* may call exit() */ } int main(int argc, char *argv[]) { int r, xfd; fd_set rd; if(argc == 2 && !strncmp("-v", argv[1], 3)) { fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); exit(EXIT_SUCCESS); } else if(argc != 1) eprint("usage: dwm [-v]\n"); dpy = XOpenDisplay(0); if(!dpy) eprint("dwm: cannot open display\n"); xfd = ConnectionNumber(dpy); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); otherwm = False; XSetErrorHandler(xerrorstart); /* this causes an error if some other window manager is running */ XSelectInput(dpy, root, SubstructureRedirectMask); XSync(dpy, False); if(otherwm) eprint("dwm: another window manager is already running\n"); XSync(dpy, False); XSetErrorHandler(NULL); xerrorxlib = XSetErrorHandler(xerror); XSync(dpy, False); setup(); drawstatus(); scan(); /* main event loop, also reads status text from stdin */ XSync(dpy, False); procevent(); readin = True; while(running) { FD_ZERO(&rd); if(readin) FD_SET(STDIN_FILENO, &rd); FD_SET(xfd, &rd); r = select(xfd + 1, &rd, NULL, NULL, NULL); if((r == -1) && (errno == EINTR)) continue; if(r > 0) { if(readin && FD_ISSET(STDIN_FILENO, &rd)) { readin = NULL != fgets(stext, sizeof(stext), stdin); if(readin) stext[strlen(stext) - 1] = 0; else strcpy(stext, "broken pipe"); drawstatus(); } } else if(r < 0) eprint("select failed\n"); procevent(); } cleanup(); XCloseDisplay(dpy); return 0; }