Scripting improvements

This commit is contained in:
Richard Frith-Macdonald 2023-04-07 12:09:24 +01:00
parent e196b76373
commit afb667be58
7 changed files with 189 additions and 29 deletions

View file

@ -1,3 +1,15 @@
2023-04-07 Richard Frith-Macdonald <rfm@gnu.org>
* ECCL.h:
* EcCommand.m:
* EcConsole.m:
* EcControl.m:
* EcProcess.h:
* EcProcess.m:
Add display options 'mark' and 'prompt' to control output format to
make it easier to perform pattern matching when using Console in a
script.
2023-03-10 Richard Frith-Macdonald <rfm@gnu.org> 2023-03-10 Richard Frith-Macdonald <rfm@gnu.org>
* EcCommand.m: * EcCommand.m:

8
ECCL.h
View file

@ -122,7 +122,13 @@
If the Control server actively refuses the login, the exit status If the Control server actively refuses the login, the exit status
is 11.<br /> is 11.<br />
NB. Command line arguments take precedence over environment variables NB. Command line arguments take precedence over environment variables
even if the command line aregument is an empty string. even if the command line aregument is an empty string.<br />
A couple of extra variables alter the format of output as follows:<br />
Mark (or the ConsoleMark environment variable) can be set to YES to
specify that each message is marked with a consistent start and end
saying where the message was from.<br />
Prompt (or the ConsolePrompt environment variable) can be set to NO
to suppress the normal prompt line at the end of each message.
</section> </section>
<section> <section>

View file

@ -5255,7 +5255,7 @@ NSLog(@"Problem %@", localException);
m = [NSString stringWithFormat: @"Unknown command - '%@'\n", wd]; m = [NSString stringWithFormat: @"Unknown command - '%@'\n", wd];
} }
[self information: m from: t to: f type: LT_CONSOLE]; [self reply: m to: f from: ecFullName()];
} }
else else
{ {
@ -6109,6 +6109,8 @@ NSLog(@"Problem %@", localException);
} }
else else
{ {
NSLog(@"reply: %@ to: %@ from: %@ - discarded (no connection to Control)",
msg, n, c);
} }
} }

View file

@ -583,7 +583,7 @@ static NSString *originalUserName = nil;
} }
} }
- (void) doLine:(NSString *)_line - (void) doLine: (NSString *)_line
{ {
NSAutoreleasePool *arp; NSAutoreleasePool *arp;
NSMutableArray *words; NSMutableArray *words;
@ -817,6 +817,9 @@ static NSString *originalUserName = nil;
if (user && pass) if (user && pass)
{ {
BOOL mark = NO;
BOOL prompt = YES;
interactive = NO; interactive = NO;
server = [NSConnection rootProxyForConnectionWithRegisteredName: name server = [NSConnection rootProxyForConnectionWithRegisteredName: name
host: host host: host
@ -835,6 +838,7 @@ static NSString *originalUserName = nil;
} }
[self cmdQuit: 10]; [self cmdQuit: 10];
DESTROY(self); DESTROY(self);
return nil;
} }
else else
{ {
@ -863,6 +867,47 @@ static NSString *originalUserName = nil;
} }
} }
if ([defs objectForKey: @"Mark"])
{
mark = [defs boolForKey: @"Mark"];
}
else
{
if ([env objectForKey: @"ConsoleMark"])
{
mark = [[env objectForKey: @"ConsoleMark"] boolValue];
}
}
/* Tell the Control server to mark start and end of messages
* if required.
*/
if (mark)
{
[self doCommand: [NSMutableArray arrayWithObjects:
@"silent", @"set", @"display", @"mark", nil]];
}
if ([defs objectForKey: @"Prompt"])
{
prompt = [defs boolForKey: @"Prompt"];
}
else
{
if ([env objectForKey: @"ConsolePrompt"])
{
prompt = [[env objectForKey: @"ConsolePrompt"] boolValue];
}
}
/* Tell the Control server not to prompt if necessary
*/
if (NO == prompt)
{
[self doCommand: [NSMutableArray arrayWithObjects:
@"silent", @"unset", @"display", @"prompt", nil]];
}
if (nil == (s = [defs stringForKey: @"Line"])) if (nil == (s = [defs stringForKey: @"Line"]))
{ {
s = [env objectForKey: @"ConsoleLine"]; s = [env objectForKey: @"ConsoleLine"];

View file

@ -204,6 +204,8 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
BOOL errors; BOOL errors;
BOOL alerts; BOOL alerts;
BOOL audits; BOOL audits;
BOOL mark;
BOOL prompt;
} }
- (id) initFor: (id)o - (id) initFor: (id)o
name: (NSString*)n name: (NSString*)n
@ -215,15 +217,19 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
- (BOOL) getAudits; - (BOOL) getAudits;
- (BOOL) getErrors; - (BOOL) getErrors;
- (BOOL) getGeneral; - (BOOL) getGeneral;
- (BOOL) getMark;
- (BOOL) getPrompt;
- (BOOL) getWarnings; - (BOOL) getWarnings;
- (NSString*) pass; - (NSString*) pass;
- (NSString*) promptAfter: (NSString*)msg; - (NSString*) promptAfter: (NSString*)msg from: (NSString*)sender;
- (void) setAlerts: (BOOL)flag; - (void) setAlerts: (BOOL)flag;
- (void) setAudits: (BOOL)flag; - (void) setAudits: (BOOL)flag;
- (void) setConnectedHost: (NSString*)c; - (void) setConnectedHost: (NSString*)c;
- (void) setConnectedServ: (NSString*)c; - (void) setConnectedServ: (NSString*)c;
- (void) setErrors: (BOOL)flag; - (void) setErrors: (BOOL)flag;
- (void) setGeneral: (BOOL)flag; - (void) setGeneral: (BOOL)flag;
- (void) setMark: (BOOL)flag;
- (void) setPrompt: (BOOL)flag;
- (void) setWarnings: (BOOL)flag; - (void) setWarnings: (BOOL)flag;
@end @end
@ -267,6 +273,16 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
return general; return general;
} }
- (BOOL) getMark
{
return mark;
}
- (BOOL) getPrompt
{
return prompt;
}
- (BOOL) getWarnings - (BOOL) getWarnings
{ {
return warnings; return warnings;
@ -288,6 +304,8 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
alerts = YES; alerts = YES;
audits = NO; audits = NO;
errors = YES; errors = YES;
mark = NO;
prompt = YES;
} }
return self; return self;
} }
@ -297,23 +315,40 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
return pass; return pass;
} }
- (NSString*) promptAfter: (NSString*)msg - (NSString*) promptAfter: (NSString*)msg from: (NSString*)sender
{ {
if (chost && cserv) NSString *sMark = @"";
NSString *eMark = @"";
if (sender && mark)
{ {
return [NSString stringWithFormat: @"%@\n%@:%@> ", msg, chost, cserv]; sMark = [NSString stringWithFormat: @"[start message from %@]\n", sender];
eMark = [NSString stringWithFormat: @"\n[end message from %@]", sender];
}
if (NO == prompt)
{
return [NSString stringWithFormat: @"%@%@%@",
sMark, msg, eMark];
}
else if (chost && cserv)
{
return [NSString stringWithFormat: @"%@%@%@\n%@:%@> ",
sMark, msg, eMark, chost, cserv];
} }
else if (chost) else if (chost)
{ {
return [NSString stringWithFormat: @"%@\n%@:> ", msg, chost]; return [NSString stringWithFormat: @"%@%@%@\n%@:> ",
sMark, msg, eMark, chost];
} }
else if (cserv) else if (cserv)
{ {
return [NSString stringWithFormat: @"%@\n:%@> ", msg, cserv]; return [NSString stringWithFormat: @"%@%@%@\n:%@> ",
sMark, msg, eMark, cserv];
} }
else else
{ {
return [NSString stringWithFormat: @"%@\nControl> ", msg]; return [NSString stringWithFormat: @"%@%@%@\nControl> ",
sMark, msg, eMark];
} }
} }
@ -347,6 +382,16 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
general = flag; general = flag;
} }
- (void) setMark: (BOOL)flag
{
mark = (flag ? YES : NO);
}
- (void) setPrompt: (BOOL)flag
{
prompt = (flag ? YES : NO);
}
- (void) setWarnings: (BOOL)flag - (void) setWarnings: (BOOL)flag
{ {
warnings = flag; warnings = flag;
@ -519,7 +564,7 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
NS_DURING NS_DURING
{ {
[[c obj] information: [c promptAfter: desc]]; [[c obj] information: [c promptAfter: desc from: nil]];
} }
NS_HANDLER NS_HANDLER
{ {
@ -689,12 +734,19 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
{ {
NSMutableArray *cmd; NSMutableArray *cmd;
ConsoleInfo *console; ConsoleInfo *console;
BOOL silent = NO;
cmd = [NSPropertyListSerialization cmd = [NSPropertyListSerialization
propertyListWithData: dat propertyListWithData: dat
options: NSPropertyListMutableContainers options: NSPropertyListMutableContainers
format: 0 format: 0
error: 0]; error: 0];
if ([[cmd firstObject] isEqual: @"silent"])
{
silent = YES;
[cmd removeObjectAtIndex: 0];
}
console = (ConsoleInfo*)[self findIn: consoles byName: f]; console = (ConsoleInfo*)[self findIn: consoles byName: f];
if (console == nil) if (console == nil)
{ {
@ -1272,6 +1324,10 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
@" displays alert (and critical severity alarm) messages.\n" @" displays alert (and critical severity alarm) messages.\n"
@" set display audits\n" @" set display audits\n"
@" displays audit messages.\n" @" displays audit messages.\n"
@" set display mark\n"
@" marks start and end of messages.\n"
@" set display prompt\n"
@" outputs a prompt at the end of each message.\n"
@"\n"; @"\n";
} }
else if (comp(wd, @"Status") >= 0) else if (comp(wd, @"Status") >= 0)
@ -1326,6 +1382,8 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
@" unset display errors\n" @" unset display errors\n"
@" unset display alerts\n" @" unset display alerts\n"
@" unset display audits\n" @" unset display audits\n"
@" unset display mark\n"
@" unset display prompt\n"
@"\n"; @"\n";
} }
} }
@ -1533,10 +1591,12 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
{ {
m = [NSString stringWithFormat: @"display settings -\n" m = [NSString stringWithFormat: @"display settings -\n"
@"general: %d warnings: %d errors: %d alerts: %d" @"general: %d warnings: %d errors: %d alerts: %d"
@" alerts: %d\n", @" audits: %d mark:%@ prompt:%@\n",
[console getGeneral], [console getWarnings], [console getGeneral], [console getWarnings],
[console getErrors], [console getAlerts], [console getErrors], [console getAlerts],
[console getAudits]]; [console getAudits],
([console getMark] ? @"yes" : @"no"),
([console getPrompt] ? @"yes" : @"no")];
} }
else if (comp(wd, @"alerts") >= 0) else if (comp(wd, @"alerts") >= 0)
{ {
@ -1554,6 +1614,14 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
{ {
[console setGeneral: YES]; [console setGeneral: YES];
} }
else if (comp(wd, @"mark") >= 0)
{
[console setMark: YES];
}
else if (comp(wd, @"prompt") >= 0)
{
[console setPrompt: YES];
}
else if (comp(wd, @"warnings") >= 0) else if (comp(wd, @"warnings") >= 0)
{ {
[console setWarnings: YES]; [console setWarnings: YES];
@ -1667,10 +1735,12 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
{ {
m = [NSString stringWithFormat: @"display settings -\n" m = [NSString stringWithFormat: @"display settings -\n"
@"general: %d warnings: %d errors: %d alerts: %d" @"general: %d warnings: %d errors: %d alerts: %d"
@" audits: %d\n", @" audits: %d mark:%@ prompt:%@\n",
[console getGeneral], [console getWarnings], [console getGeneral], [console getWarnings],
[console getErrors], [console getAlerts], [console getErrors], [console getAlerts],
[console getAudits]]; [console getAudits],
([console getMark] ? @"yes" : @"no"),
([console getPrompt] ? @"yes" : @"no")];
} }
else if (comp(wd, @"alerts") >= 0) else if (comp(wd, @"alerts") >= 0)
{ {
@ -1688,6 +1758,14 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
{ {
[console setGeneral: NO]; [console setGeneral: NO];
} }
else if (comp(wd, @"mark") >= 0)
{
[console setMark: NO];
}
else if (comp(wd, @"prompt") >= 0)
{
[console setPrompt: NO];
}
else if (comp(wd, @"warnings") >= 0) else if (comp(wd, @"warnings") >= 0)
{ {
[console setWarnings: NO]; [console setWarnings: NO];
@ -1707,12 +1785,12 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
m = [NSString stringWithFormat: @"Unknown command - '%@'\n", wd]; m = [NSString stringWithFormat: @"Unknown command - '%@'\n", wd];
} }
if (m != nil) if (m != nil && NO == silent)
{ {
[self information: m [self information: m
type: LT_CONSOLE type: LT_CONSOLE
to: [console name] to: [console name]
from: nil]; from: ecFullName()];
} }
} }
} }
@ -1998,11 +2076,10 @@ static NSString* cmdWord(NSArray* a, unsigned int pos)
NS_DURING NS_DURING
{ {
/* /* Finally - we send the message to the console along with
* Finally - we send the message to the console along with * a prompt.
* a prompt.
*/ */
[[c obj] information: [c promptAfter: inf]]; [[c obj] information: [c promptAfter: inf from: from]];
} }
NS_HANDLER NS_HANDLER
{ {

View file

@ -323,13 +323,14 @@ typedef enum {
/* /*
* Useful functions - * Useful functions -
*/ */
extern void cmdSetHome(NSString *home); extern void cmdSetHome(NSString *home);
extern NSString* cmdDataDir(); extern NSString *cmdDataDir();
extern NSString* cmdLogsDir(NSString *date); extern NSString *cmdLogsDir(NSString *date);
extern NSString* cmdLogKey(EcLogType t); extern NSString *cmdLogKey(EcLogType t);
extern NSString* cmdLogName(); extern NSString *cmdLogName();
extern NSString* cmdLogFormat(EcLogType t, NSString *fmt); extern NSString *cmdLogFormat(EcLogType t, NSString *fmt);
extern void ecSetLogsSubdirectory(NSString *pathComponent); extern NSString *ecFullName();
extern void ecSetLogsSubdirectory(NSString *pathComponent);
/** Return the native thread ID of the current thread, or NSNotFound if /** Return the native thread ID of the current thread, or NSNotFound if
* that is not available. * that is not available.

View file

@ -888,6 +888,23 @@ ecHostName()
return [name autorelease]; return [name autorelease];
} }
static NSString *fullName = nil;
NSString *
ecFullName()
{
NSString *name;
[ecLock lock];
if (nil == fullName)
{
fullName = [[NSString alloc] initWithFormat: @"%@:%@",
ecHostName(), cmdLogName()];
}
name = [fullName retain];
[ecLock unlock];
return [name autorelease];
}
static EcAlarmSeverity memAlarm = EcAlarmSeverityMajor; static EcAlarmSeverity memAlarm = EcAlarmSeverityMajor;
static NSString *memType = nil; static NSString *memType = nil;
static NSString *memUnit = @"KB"; static NSString *memUnit = @"KB";
@ -4418,7 +4435,7 @@ NSLog(@"Ignored attempt to set timer interval to %g ... using 10.0", interval);
{ {
NS_DURING NS_DURING
{ {
[cmdServer reply: val to: name from: cmdLogName()]; [cmdServer reply: val to: name from: ecFullName()];
} }
NS_HANDLER NS_HANDLER
{ {