Changeset 40 in ntrip for trunk/ntripclient


Ignore:
Timestamp:
Jun 22, 2006, 1:57:14 PM (14 years ago)
Author:
stoecker
Message:

added URL style

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ntripclient/NtripLinuxClient.c

    r31 r40  
    11/*
    22  Easy example NTRIP client for Linux/Unix.
    3   $Id: NtripLinuxClient.c,v 1.15 2005/12/06 16:50:26 euronav Exp $
     3  $Id: NtripLinuxClient.c,v 1.16 2006/04/27 09:44:17 stoecker Exp $
    44  Copyright (C) 2003-2005 by Dirk Stoecker <soft@dstoecker.de>
    55   
     
    2222#include <ctype.h>
    2323#include <getopt.h>
     24#include <signal.h>
    2425#include <stdio.h>
    2526#include <stdlib.h>
     
    3637
    3738#define MAXDATASIZE 1000 /* max number of bytes we can get at once */
     39#define ALARMTIME   (2*60)
    3840
    3941/* CVS revision and version */
    40 static char revisionstr[] = "$Revision: 1.15 $";
    41 static char datestr[]     = "$Date: 2005/12/06 16:50:26 $";
     42static char revisionstr[] = "$Revision: 1.16 $";
     43static char datestr[]     = "$Date: 2006/04/27 09:44:17 $";
    4244
    4345struct Args
     
    6466{0,0,0,0}};
    6567#endif
    66 #define ARGOPT "d:hp:r:s:u:"
     68#define ARGOPT "-d:hp:r:s:u:"
     69
     70#ifdef __GNUC__
     71static __attribute__ ((noreturn)) void sighandler_alarm(
     72int sig __attribute__((__unused__)))
     73#else /* __GNUC__ */
     74static void sighandler_alarm(int sig)
     75#endif /* __GNUC__ */
     76{
     77  fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
     78  exit(1);
     79}
     80
     81static const char *geturl(const char *url, struct Args *args)
     82{
     83  static char buf[1000];
     84  static char *Buffer = buf;
     85  static char *Bufend = buf+sizeof(buf);
     86
     87  if(strncmp("ntrip:", url, 6))
     88    return "URL must start with 'ntrip:'.";
     89  url += 6; /* skip ntrip: */
     90
     91  /* scan for mountpoint */
     92  args->data = Buffer;
     93  while(*url && *url != '@' && *url != '/' && Buffer != Bufend)
     94    *(Buffer++) = *(url++);
     95  if(Buffer == args->data)
     96    return "Mountpoint required.";
     97  else if(Buffer >= Bufend-1)
     98    return "Parsing buffer too short.";
     99  *(Buffer++) = 0;
     100
     101  if(*url == '/') /* username and password */
     102  {
     103    ++url;
     104    args->user = Buffer;
     105    while(*url && *url != '@' && *url != ':' && Buffer != Bufend)
     106      *(Buffer++) = *(url++);
     107    if(Buffer == args->user)
     108      return "Username cannot be empty.";
     109    else if(Buffer >= Bufend-1)
     110      return "Parsing buffer too short.";
     111    *(Buffer++) = 0;
     112
     113    if(*url == ':') ++url;
     114
     115    args->password = Buffer;
     116    while(*url && *url != '@' && Buffer != Bufend)
     117      *(Buffer++) = *(url++);
     118    if(Buffer == args->password)
     119      return "Password cannot be empty.";
     120    else if(Buffer >= Bufend-1)
     121      return "Parsing buffer too short.";
     122    *(Buffer++) = 0;
     123  }
     124
     125  if(*url == '@') /* server */
     126  {
     127    ++url;
     128    args->server = Buffer;
     129    while(*url && *url != ':' && Buffer != Bufend)
     130      *(Buffer++) = *(url++);
     131    if(Buffer == args->server)
     132      return "Servername cannot be empty.";
     133    else if(Buffer >= Bufend-1)
     134      return "Parsing buffer too short.";
     135    *(Buffer++) = 0;
     136
     137    if(*url == ':')
     138    {
     139      char *s2 = 0;
     140      args->port = strtol(++url, &s2, 10);
     141      if(*s2 || args->port <= 0 || args->port > 0xFFFF)
     142        return "Illegal port number.";
     143      url = s2;
     144    }
     145  }
     146
     147  return *url ? "Garbage at end of server string." : 0;
     148}
    67149
    68150static int getargs(int argc, char **argv, struct Args *args)
     
    94176    case 'd': args->data = optarg; break;
    95177    case 'h': help=1; break;
     178    case 1:
     179      {
     180        const char *err;
     181        if((err = geturl(optarg, args)))
     182        {
     183          fprintf(stderr, "%s\n\n", err);
     184          res = 0;
     185        }
     186      }
     187      break;
    96188    case 'r':
    97189      args->port = strtoul(optarg, &t, 10);
     
    101193    case -1: break;
    102194    }
    103   } while(getoptr != -1 || !res);
     195  } while(getoptr != -1 && res);
    104196
    105197  for(a = revisionstr+11; *a && *a != ' '; ++a)
     
    119211  if(!res || help)
    120212  {
    121     fprintf(stderr, "Version %s (%s) GPL\nUsage: %s -s server -u user ...\n"
     213    fprintf(stderr, "Version %s (%s) GPL\nUsage:\n%s -s server -u user ...\n"
    122214    " -d " LONG_OPT("--data     ") "the requested data set\n"
    123215    " -s " LONG_OPT("--server   ") "the server name or address\n"
     
    125217    " -r " LONG_OPT("--port     ") "the server port number (default 80)\n"
    126218    " -u " LONG_OPT("--user     ") "the user name\n"
    127     , revisionstr, datestr, argv[0]);
     219    "or using an URL:\n%s ntrip:mountpoint[/username[:password]][@server[:port]]\n"
     220    , revisionstr, datestr, argv[0], argv[0]);
    128221    exit(1);
    129222  }
     
    187280  setbuf(stdin, 0);
    188281  setbuf(stderr, 0);
     282  signal(SIGALRM,sighandler_alarm);
     283  alarm(ALARMTIME);
    189284
    190285  if(getargs(argc, argv, &args))
     
    261356    {
    262357      int k = 0;
     358
    263359      while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
    264360      {
     361        alarm(ALARMTIME);
    265362        if(!k)
    266363        {
Note: See TracChangeset for help on using the changeset viewer.