diff src/noffle.c @ 150:1c7303c71f66 noffle

[svn] * src/protocol.c: Fix bug in Prt_getLn if we should read a line starting with '\0' - according to the leafnode mailing list, this has been seen in the wild. * docs/inews.1,docs/noffle.1,docs/noffle.conf.5, packages/redhat/noffle.spec,src/configfile.h,src/configfile.c, src/noffle.c,src/post.h,src/post.c: Removed use of getopt_long, and added inews mode - the Noffle executable behaves as inews is invoked as inews. This includes adding From: and Organization: headers if necessary - add configs to override defaults for the From: domain and specify the organization. For all my fellow trn-heads out there, and users of any other ageing newsreader that expects inews. Updated RPM spec to create inews link to noffle on install.
author bears
date Thu, 26 Oct 2000 22:21:13 +0100
parents a740dac296bc
children 8ea6b5ddc5a5
line wrap: on
line diff
--- a/src/noffle.c	Thu Oct 26 22:13:28 2000 +0100
+++ b/src/noffle.c	Thu Oct 26 22:21:13 2000 +0100
@@ -10,7 +10,7 @@
   received for some seconds (to allow multiple clients connect at the same
   time).
 
-  $Id: noffle.c 197 2000-08-14 19:49:56Z bears $
+  $Id: noffle.c 227 2000-10-26 21:21:13Z bears $
 */
 
 #if HAVE_CONFIG_H
@@ -19,7 +19,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <getopt.h>
 #include <signal.h>
 #include <sys/time.h>
 #include <sys/resource.h>
@@ -182,18 +181,18 @@
 }
 
 static Bool
-doPost( void )
+doPost( FILE *f, unsigned flags )
 {
     Str line;
     DynStr *s;
     Bool res;
 
     s = new_DynStr( 10000 );
-    while ( fgets( line, MAXCHAR, stdin ) != NULL )
+    while ( fgets( line, MAXCHAR, f ) != NULL )
 	DynStr_app( s, line );
 
     res = TRUE;
-    if ( ! Post_open( DynStr_str( s ) ) )
+    if ( ! Post_open( DynStr_str( s ), flags ) )
     {
 	fprintf( stderr, "Post failed: Malformed article.\n" );
 	res = FALSE;
@@ -640,36 +639,144 @@
     raise( sig );
 }
 
+static void
+printInewsUsage( void )
+{
+    static const char *msg =
+	"Usage: inews [-D] [-O] [-S] [input]\n"
+	" -D    Debug - send article to stdout and don't post\n"
+	" -O    Don't add Organization header\n"
+	" -S    Don't add .signature\n"
+	" input File containing message, standard input if none specified.\n"
+	"For compatability, -h, -A, -V, -W are ignored and -N is\n"
+	"equivalent to -D.\n";
+    fprintf( stderr, "%s", msg );
+}
+
+static int
+doInews( int argc, char **argv )
+{
+    int result;
+    int flags;
+    FILE *f;
+
+    UNUSED( argc );
+    
+    noffle.lockAtStartup = TRUE;
+
+    /* Process options */
+    flags = POST_ADD_ORG | POST_ADD_SIG | POST_ADD_FROM;
+    for ( ; argv[0] != NULL && argv[0][0] == '-' ; argv++ )
+    {
+	if ( argv[0][2] != '\0' )
+	{
+	    printInewsUsage();
+	    return EXIT_FAILURE;
+	}
+	
+	switch( argv[0][1] )
+	{
+	case 'h':
+	case 'A':
+	case 'V':
+	case 'W':
+	    break;
+	case 'N':
+	case 'D':
+	    flags |= POST_DEBUG;
+	    break;
+	case 'O':
+	    flags &= ~POST_ADD_ORG;
+	    break;
+	case 'S':
+	    flags &= ~POST_ADD_SIG;
+	    break;
+	default:
+	    printInewsUsage();
+	    return EXIT_FAILURE;
+	}
+    }
+    
+    if ( argv[0] ==  NULL )
+	f = stdin;
+    else
+    {
+	f = fopen( argv[0], "r" );
+	if ( f == NULL )
+	{
+	    Log_err( "Can't access %s (%s).", argv[0], strerror( errno ) );
+	    return EXIT_FAILURE;
+	}
+    }
+
+    if ( ! initNoffle() )
+	return EXIT_FAILURE;
+    result = EXIT_SUCCESS;
+
+    if ( ! doPost( f, flags ) )
+	result = EXIT_FAILURE;
+
+    if ( f != stdin )
+	fclose( f );
+
+    closeNoffle();
+    return result;
+}
+
+static int
+getArgLetter(const char *arg)
+{
+    int res;
+    struct option
+    {
+	const char *longOpt;
+	const char *opt;
+    } options[] =
+    {
+	{ "--article", 		"-a" },
+	{ "--cancel", 		"-c" },
+	{ "--create", 		"-C" },
+	{ "--database",		"-d" },
+	{ "--delete", 		"-D" },
+	{ "--expire", 		"-e" },
+	{ "--fetch", 		"-f" },
+	{ "--groups", 		"-g" },
+	{ "--help", 		"-h" },
+	{ "--list", 		"-l" },
+	{ "--modify", 		"-m" },
+	{ "--offline", 		"-o" },
+	{ "--online", 		"-n" },
+	{ "--post", 		"-p" },
+	{ "--query", 		"-q" },
+	{ "--server",		"-r" },
+	{ "--requested",	"-R" },
+	{ "--subscribe-over",	"-s" },
+	{ "--subscribe-full",	"-S" },
+	{ "--subscribe-thread",	"-t" },
+	{ "--unsubscribe",	"-u" },
+	{ "--version",		"-v" },
+	{ NULL,			NULL }
+	
+    };
+    struct option *opt;
+
+    res = -1;
+    for ( opt = options; opt->opt != NULL; opt++ )
+	if ( strcmp( arg, opt->longOpt ) == 0 ||
+	     strcmp( arg, opt->opt ) == 0 )
+	{
+	    res = opt->opt[1];
+	    break;
+	}
+
+    return res;
+}
+
 int main ( int argc, char **argv )
 {
     int c, result;
-    struct option longOptions[] =
-    {
-        { "article",          required_argument, NULL, 'a' },
-        { "cancel",           required_argument, NULL, 'c' },
-        { "create",           required_argument, NULL, 'C' },
-        { "database",         no_argument,       NULL, 'd' },
-        { "delete",           required_argument, NULL, 'D' },
-        { "expire",           no_argument,       NULL, 'e' },
-        { "fetch",            no_argument,       NULL, 'f' },
-        { "groups",           no_argument,       NULL, 'g' },
-        { "help",             no_argument,       NULL, 'h' },
-        { "list",             no_argument,       NULL, 'l' },
-        { "modify",           required_argument, NULL, 'm' },
-        { "offline",          no_argument,       NULL, 'o' },
-        { "online",           no_argument,       NULL, 'n' },
-        { "post",             no_argument, 	 NULL, 'p' },
-        { "query",            required_argument, NULL, 'q' },
-        { "server",           no_argument,       NULL, 'r' },
-        { "requested",        no_argument,       NULL, 'R' },
-        { "subscribe-over",   required_argument, NULL, 's' },
-        { "subscribe-full",   required_argument, NULL, 'S' },
-        { "subscribe-thread", required_argument, NULL, 't' },
-        { "unsubscribe",      required_argument, NULL, 'u' },
-        { "version",          no_argument,       NULL, 'v' },
-        { NULL, 0, NULL, 0 }
-    };
-    
+    const char *cmdname, *p;
+
     signal( SIGSEGV, bugReport );
     signal( SIGABRT, logSignal );
     signal( SIGFPE, logSignal );
@@ -677,55 +784,72 @@
     signal( SIGINT, logSignal );
     signal( SIGTERM, logSignal );
     signal( SIGPIPE, logSignal );
-    c = getopt_long( argc, argv, "a:c:C:dD:efghlm:onpq:rRs:S:t:u:v",
-                     longOptions, NULL );
+
+    /* Find last component of command name. */
+    cmdname = argv[0];
+    p = strrchr( cmdname, '/' );
+    if ( p != NULL )
+	cmdname = ++p;
+
+    argv++;
+    argc--;
+
+    /* Were we invoked as inews? */
+    if ( strcmp( cmdname, "inews" ) == 0 )
+	return doInews( argc, argv );
+
+    c = -1;
+    if ( *argv != NULL )
+    {
+	c = getArgLetter( *argv );
+	argv++;
+	argc--;
+    }
+
     noffle.lockAtStartup = ! ( c == 'r' || c == 'h' );
     if ( ! initNoffle() )
         return EXIT_FAILURE;
     result = EXIT_SUCCESS;
     switch ( c )
     {
-    case 0:
-        /* Options that set a flag. */
-        break;
     case 'a':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -a needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            doArt( optarg );
+            doArt( *argv );
         break;
     case 'c':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -c needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            doCancel( optarg );
+            doCancel( *argv );
         break;
     case 'C':
-        if ( ! optarg )
+        if ( *argv == NULL  )
         {
             fprintf( stderr, "Option -C needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            doCreateLocalGroup( optarg );
+            doCreateLocalGroup( *argv );
         break;
     case 'd':
         doDb();
         break;
     case 'D':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -D needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            doDeleteLocalGroup( optarg );
+            doDeleteLocalGroup( *argv );
         break;
     case 'e':
 	doExpire();
@@ -744,13 +868,13 @@
         doList();
         break;
     case 'm':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -m needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-	    if ( ! doModify( optarg, argc - optind, &argv[ optind ] ) )
+	    if ( ! doModify( *argv, --argc, ++argv ) )
 		result = EXIT_FAILURE;
         break;
     case 'n':
@@ -766,24 +890,24 @@
             Online_set( FALSE );
         break;
     case 'p':
-	if ( ! doPost() )
+	if ( ! doPost( stdin, 0 ) )
 	    result = EXIT_FAILURE;
         break;
     case 'q':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -q needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
         {
-            if ( strcmp( optarg, "groups" ) == 0 )
+            if ( strcmp( *argv, "groups" ) == 0 )
                 noffle.queryGrps = TRUE;
-            else if ( strcmp( optarg, "desc" ) == 0 )
+            else if ( strcmp( *argv, "desc" ) == 0 )
                 noffle.queryDsc = TRUE;
             else
             {
-                fprintf( stderr, "Unknown argument -q %s\n", optarg );
+                fprintf( stderr, "Unknown argument -q %s\n", *argv );
                 result = EXIT_FAILURE;
             }
             doQuery();
@@ -794,43 +918,43 @@
         Server_run();
         break;
     case 'R':
-        doRequested( optarg );
+        doRequested( *argv );
         break;
     case 's':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -s needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            result = doSubscribe( optarg, OVER );
+            result = doSubscribe( *argv, OVER );
         break;
     case 'S':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -S needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            doSubscribe( optarg, FULL );
+            doSubscribe( *argv, FULL );
         break;
     case 't':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -t needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            result = doSubscribe( optarg, THREAD );
+            result = doSubscribe( *argv, THREAD );
         break;
     case 'u':
-        if ( ! optarg )
+        if ( *argv == NULL )
         {
             fprintf( stderr, "Option -u needs argument.\n" );
             result = EXIT_FAILURE;
         }
         else
-            doUnsubscribe( optarg );
+            doUnsubscribe( *argv );
         break;
     case '?':
         /* Error message already printed by getopt_long */