Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
htop
Commits
62a31673
Commit
62a31673
authored
Feb 25, 2010
by
Hisham Muhammad
Browse files
getopt-based long options and --no-color
(thanks to Vincent Launchbury)
parent
02a30bfe
Changes
2
Show whitespace changes
Inline
Side-by-side
ChangeLog
View file @
62a31673
...
@@ -5,6 +5,8 @@ What's new in version 0.8.4
...
@@ -5,6 +5,8 @@ What's new in version 0.8.4
(thanks to Anders Torger)
(thanks to Anders Torger)
* Fix license terms with regard to PLPA
* Fix license terms with regard to PLPA
(thanks to Tom Callaway)
(thanks to Tom Callaway)
* getopt-based long options and --no-color
(thanks to Vincent Launchbury)
What's new in version 0.8.3
What's new in version 0.8.3
...
...
htop.c
View file @
62a31673
/*
/*
htop - htop.c
htop - htop.c
(C) 2004-200
8
Hisham H. Muhammad
(C) 2004-20
1
0 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
in the source distribution for its full text.
*/
*/
...
@@ -12,6 +12,7 @@ in the source distribution for its full text.
...
@@ -12,6 +12,7 @@ in the source distribution for its full text.
#include <ctype.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdbool.h>
#include <locale.h>
#include <locale.h>
#include <getopt.h>
#include "ProcessList.h"
#include "ProcessList.h"
#include "CRT.h"
#include "CRT.h"
...
@@ -36,22 +37,29 @@ in the source distribution for its full text.
...
@@ -36,22 +37,29 @@ in the source distribution for its full text.
#define INCSEARCH_MAX 40
#define INCSEARCH_MAX 40
#define COPYRIGHT "(C) 2004-2010 Hisham Muhammad"
static
void
printVersionFlag
()
{
static
void
printVersionFlag
()
{
clear
();
fputs
(
"htop "
VERSION
" - "
COPYRIGHT
"
\n
"
printf
(
"htop "
VERSION
" - (C) 2004-2008 Hisham Muhammad.
\n
"
);
"Released under the GNU GPL.
\n\n
"
,
printf
(
"Released under the GNU GPL.
\n\n
"
);
stdout
);
exit
(
0
);
exit
(
0
);
}
}
static
void
printHelpFlag
()
{
static
void
printHelpFlag
()
{
clear
();
fputs
(
"htop "
VERSION
" - "
COPYRIGHT
"
\n
"
printf
(
"htop "
VERSION
" - (C) 2004-2008 Hisham Muhammad.
\n
"
);
"Released under the GNU GPL.
\n\n
"
printf
(
"Released under the GNU GPL.
\n\n
"
);
"-C --no-color Use a monochrome color scheme
\n
"
printf
(
"-d DELAY Delay between updates, in tenths of seconds
\n\n
"
);
"-d --delay=DELAY Set the delay between updates, in tenths of seconds
\n
"
printf
(
"-u USERNAME Show only processes of a given user
\n\n
"
);
"-h --help Print this help screen
\n
"
printf
(
"--sort-key COLUMN Sort by this column (use --sort-key help for a column list)
\n\n
"
);
"-s --sort-key=COLUMN Sort by COLUMN (try --sort-key=help for a list)
\n
"
printf
(
"Press F1 inside htop for online help.
\n
"
);
"-u --user=USERNAME Show only processes of a given user
\n
"
printf
(
"See the man page for full information.
\n\n
"
);
"-v --version Print version info
\n
"
"
\n
"
"Long options may be passed with a single dash.
\n\n
"
"Press F1 inside htop for online help.
\n
"
"See 'man htop' for more information.
\n
"
,
stdout
);
exit
(
0
);
exit
(
0
);
}
}
...
@@ -62,7 +70,7 @@ static void showHelp(ProcessList* pl) {
...
@@ -62,7 +70,7 @@ static void showHelp(ProcessList* pl) {
for
(
int
i
=
0
;
i
<
LINES
-
1
;
i
++
)
for
(
int
i
=
0
;
i
<
LINES
-
1
;
i
++
)
mvhline
(
i
,
0
,
' '
,
COLS
);
mvhline
(
i
,
0
,
' '
,
COLS
);
mvaddstr
(
0
,
0
,
"htop "
VERSION
" -
(C) 2004-2008 Hisham Muhammad."
);
mvaddstr
(
0
,
0
,
"htop "
VERSION
" -
"
COPYRIGHT
);
mvaddstr
(
1
,
0
,
"Released under the GNU GPL. See 'man' page for more info."
);
mvaddstr
(
1
,
0
,
"Released under the GNU GPL. See 'man' page for more info."
);
attrset
(
CRT_colors
[
DEFAULT_COLOR
]);
attrset
(
CRT_colors
[
DEFAULT_COLOR
]);
...
@@ -139,7 +147,7 @@ static void showHelp(ProcessList* pl) {
...
@@ -139,7 +147,7 @@ static void showHelp(ProcessList* pl) {
mvaddstr
(
18
,
0
,
" a:"
);
mvaddstr
(
18
,
0
,
" a:"
);
#endif
#endif
mvaddstr
(
19
,
0
,
" F2 S"
);
mvaddstr
(
19
,
40
,
" F6 >"
);
mvaddstr
(
19
,
0
,
" F2 S"
);
mvaddstr
(
19
,
40
,
" F6 >"
);
mvaddstr
(
20
,
0
,
"
F1 h"
);
mvaddstr
(
20
,
40
,
" l"
);
mvaddstr
(
20
,
0
,
"
?
F1 h"
);
mvaddstr
(
20
,
40
,
" l"
);
mvaddstr
(
21
,
0
,
" F10 q"
);
mvaddstr
(
21
,
40
,
" s"
);
mvaddstr
(
21
,
0
,
" F10 q"
);
mvaddstr
(
21
,
40
,
" s"
);
attrset
(
CRT_colors
[
DEFAULT_COLOR
]);
attrset
(
CRT_colors
[
DEFAULT_COLOR
]);
...
@@ -238,6 +246,20 @@ int main(int argc, char** argv) {
...
@@ -238,6 +246,20 @@ int main(int argc, char** argv) {
int
delay
=
-
1
;
int
delay
=
-
1
;
bool
userOnly
=
false
;
bool
userOnly
=
false
;
uid_t
userId
=
0
;
uid_t
userId
=
0
;
int
usecolors
=
1
;
int
opt
,
opti
=
0
;
static
struct
option
long_opts
[]
=
{
{
"help"
,
no_argument
,
0
,
'h'
},
{
"version"
,
no_argument
,
0
,
'v'
},
{
"delay"
,
required_argument
,
0
,
'd'
},
{
"sort-key"
,
required_argument
,
0
,
's'
},
{
"user"
,
required_argument
,
0
,
'u'
},
{
"no-color"
,
no_argument
,
0
,
'C'
},
{
"no-colour"
,
no_argument
,
0
,
'C'
},
{
0
,
0
,
0
,
0
}
};
int
sortKey
=
0
;
int
sortKey
=
0
;
char
*
lc_ctype
=
getenv
(
"LC_CTYPE"
);
char
*
lc_ctype
=
getenv
(
"LC_CTYPE"
);
...
@@ -246,43 +268,44 @@ int main(int argc, char** argv) {
...
@@ -246,43 +268,44 @@ int main(int argc, char** argv) {
else
else
setlocale
(
LC_CTYPE
,
getenv
(
"LC_ALL"
));
setlocale
(
LC_CTYPE
,
getenv
(
"LC_ALL"
));
int
arg
=
1
;
/* Parse arguments */
while
(
arg
<
argc
)
{
while
((
opt
=
getopt_long_only
(
argc
,
argv
,
"hvCs:d:u:"
,
long_opts
,
&
opti
)))
{
if
(
String_eq
(
argv
[
arg
],
"--help"
))
{
if
(
opt
==
EOF
)
break
;
switch
(
opt
)
{
case
'h'
:
printHelpFlag
();
printHelpFlag
();
}
else
if
(
String_eq
(
argv
[
arg
],
"--version"
))
{
break
;
case
'v'
:
printVersionFlag
();
printVersionFlag
();
}
else
if
(
String_eq
(
argv
[
arg
],
"--sort-key"
))
{
break
;
if
(
arg
==
argc
-
1
)
printHelpFlag
();
case
's'
:
arg
++
;
if
(
strcmp
(
optarg
,
"help"
))
{
char
*
field
=
argv
[
arg
];
if
(
String_eq
(
field
,
"help"
))
{
for
(
int
j
=
1
;
j
<
LAST_PROCESSFIELD
;
j
++
)
for
(
int
j
=
1
;
j
<
LAST_PROCESSFIELD
;
j
++
)
printf
(
"%s
\n
"
,
Process_fieldNames
[
j
]);
printf
(
"%s
\n
"
,
Process_fieldNames
[
j
]);
exit
(
0
);
exit
(
0
);
}
}
sortKey
=
ColumnsPanel_fieldNameToIndex
(
field
);
sortKey
=
ColumnsPanel_fieldNameToIndex
(
optarg
);
if
(
sortKey
==
-
1
)
{
if
(
sortKey
==
-
1
)
{
fprintf
(
stderr
,
"Error: invalid column
\"
%s
\"
.
\n
"
,
field
);
fprintf
(
stderr
,
"Error: invalid column
\"
%s
\"
.
\n
"
,
optarg
);
exit
(
1
);
exit
(
1
);
}
}
}
else
if
(
String_eq
(
argv
[
arg
],
"-d"
))
{
break
;
if
(
arg
==
argc
-
1
)
printHelpFlag
();
case
'd'
:
arg
++
;
sscanf
(
optarg
,
"%d"
,
&
delay
);
sscanf
(
argv
[
arg
],
"%d"
,
&
delay
);
if
(
delay
<
1
)
delay
=
1
;
if
(
delay
<
1
)
delay
=
1
;
if
(
delay
>
100
)
delay
=
100
;
if
(
delay
>
100
)
delay
=
100
;
}
else
if
(
String_eq
(
argv
[
arg
],
"-u"
))
{
break
;
if
(
arg
==
argc
-
1
)
printHelpFlag
();
case
'u'
:
arg
++
;
setUserOnly
(
optarg
,
&
userOnly
,
&
userId
)
;
setUserOnly
(
argv
[
arg
],
&
userOnly
,
&
userId
)
;
break
;
}
else
{
case
'C'
:
fprintf
(
stderr
,
"Error: unknown flag: %s
\n
"
,
argv
[
arg
])
;
usecolors
=
0
;
exit
(
1
)
;
break
;
}
}
arg
++
;
}
}
if
(
access
(
PROCDIR
,
R_OK
)
!=
0
)
{
if
(
access
(
PROCDIR
,
R_OK
)
!=
0
)
{
fprintf
(
stderr
,
"Error: could not read procfs (compiled to look in %s).
\n
"
,
PROCDIR
);
fprintf
(
stderr
,
"Error: could not read procfs (compiled to look in %s).
\n
"
,
PROCDIR
);
exit
(
1
);
exit
(
1
);
...
@@ -315,6 +338,8 @@ int main(int argc, char** argv) {
...
@@ -315,6 +338,8 @@ int main(int argc, char** argv) {
// FIXME: move delay code to settings
// FIXME: move delay code to settings
if
(
delay
!=
-
1
)
if
(
delay
!=
-
1
)
settings
->
delay
=
delay
;
settings
->
delay
=
delay
;
if
(
!
usecolors
)
settings
->
colorScheme
=
COLORSCHEME_MONOCHROME
;
CRT_init
(
settings
->
delay
,
settings
->
colorScheme
);
CRT_init
(
settings
->
delay
,
settings
->
colorScheme
);
...
@@ -532,6 +557,7 @@ int main(int argc, char** argv) {
...
@@ -532,6 +557,7 @@ int main(int argc, char** argv) {
}
}
case
KEY_F
(
1
):
case
KEY_F
(
1
):
case
'h'
:
case
'h'
:
case
'?'
:
{
{
showHelp
(
pl
);
showHelp
(
pl
);
FunctionBar_draw
(
defaultBar
,
NULL
);
FunctionBar_draw
(
defaultBar
,
NULL
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment