Adopting arg.h with more flexible cmd handling.

dev
Christoph Lohmann 2013-04-03 21:00:38 +02:00
parent 580c8bbd46
commit b1813b14d9
2 changed files with 111 additions and 60 deletions

55
arg.h 100644
View File

@ -0,0 +1,55 @@
/*
* Copy me if you can.
* by 20h
*/
#ifndef __ARG_H__
#define __ARG_H__
extern char *argv0;
#define USED(x) ((void)(x))
/* use main(int argc, char *argv[]) */
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
argv[0] && argv[0][1]\
&& argv[0][0] == '-';\
argc--, argv++) {\
char _argc;\
char **_argv;\
int brk;\
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
argv++;\
argc--;\
break;\
}\
for (brk = 0, argv[0]++, _argv = argv;\
argv[0][0] && !brk;\
argv[0]++) {\
if (_argv != argv)\
break;\
_argc = argv[0][0];\
switch (_argc)
#define ARGEND }\
USED(_argc);\
}\
USED(argv);\
USED(argc);
#define ARGC() _argc
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
((x), abort(), (char *)0) :\
(brk = 1, (argv[0][1] != '\0')?\
(&argv[0][1]) :\
(argc--, argv++, argv[0])))
#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
(char *)0 :\
(brk = 1, (argv[0][1] != '\0')?\
(&argv[0][1]) :\
(argc--, argv++, argv[0])))
#endif

116
st.c
View File

@ -27,6 +27,10 @@
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <fontconfig/fontconfig.h> #include <fontconfig/fontconfig.h>
#include "arg.h"
char *argv0;
#define Glyph Glyph_ #define Glyph Glyph_
#define Font Font_ #define Font Font_
#define Draw XftDraw * #define Draw XftDraw *
@ -41,10 +45,6 @@
#include <libutil.h> #include <libutil.h>
#endif #endif
#define USAGE \
"st " VERSION " (c) 2010-2013 st engineers\n" \
"usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
" [-t title] [-w windowid] [-e command ...]\n"
/* XEMBED messages */ /* XEMBED messages */
#define XEMBED_FOCUS_IN 4 #define XEMBED_FOCUS_IN 4
@ -3308,70 +3308,66 @@ run(void) {
} }
} }
void
usage(void) {
die("%s " VERSION " (c) 2010-2013 st engineers\n" \
"usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
" [-t title] [-w windowid] [-e command ...]\n", argv0);
}
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
int i, bitm, xr, yr; int bitm, xr, yr;
uint wr, hr; uint wr, hr;
xw.fw = xw.fh = xw.fx = xw.fy = 0; xw.fw = xw.fh = xw.fx = xw.fy = 0;
xw.isfixed = False; xw.isfixed = False;
for(i = 1; i < argc; i++) { ARGBEGIN {
switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) { case 'a':
case 'a': allowaltscreen = false;
allowaltscreen = false; break;
break; case 'c':
case 'c': opt_class = EARGF(usage());
if(++i < argc) break;
opt_class = argv[i]; case 'e':
break; /* eat all remaining arguments */
case 'e': opt_cmd = &argv[1];
/* eat all remaining arguments */ goto run;
if(++i < argc) case 'f':
opt_cmd = &argv[i]; opt_font = EARGF(usage());
goto run; break;
case 'f': case 'g':
if(++i < argc) bitm = XParseGeometry(EARGF(usage()), &xr, &yr, &wr, &hr);
opt_font = argv[i]; if(bitm & XValue)
break; xw.fx = xr;
case 'g': if(bitm & YValue)
if(++i >= argc) xw.fy = yr;
break; if(bitm & WidthValue)
xw.fw = (int)wr;
if(bitm & HeightValue)
xw.fh = (int)hr;
if(bitm & XNegative && xw.fx == 0)
xw.fx = -1;
if(bitm & XNegative && xw.fy == 0)
xw.fy = -1;
bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr); if(xw.fh != 0 && xw.fw != 0)
if(bitm & XValue) xw.isfixed = True;
xw.fx = xr; break;
if(bitm & YValue) case 'o':
xw.fy = yr; opt_io = EARGF(usage());
if(bitm & WidthValue) break;
xw.fw = (int)wr; case 't':
if(bitm & HeightValue) opt_title = EARGF(usage());
xw.fh = (int)hr; break;
if(bitm & XNegative && xw.fx == 0) case 'w':
xw.fx = -1; opt_embed = EARGF(usage());
if(bitm & XNegative && xw.fy == 0) break;
xw.fy = -1; case 'v':
default:
if(xw.fh != 0 && xw.fw != 0) usage();
xw.isfixed = True; } ARGEND;
break;
case 'o':
if(++i < argc)
opt_io = argv[i];
break;
case 't':
if(++i < argc)
opt_title = argv[i];
break;
case 'v':
default:
die(USAGE);
case 'w':
if(++i < argc)
opt_embed = argv[i];
break;
}
}
run: run:
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");