File: args.c

package info (click to toggle)
sn 0.3.4a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 784 kB
  • ctags: 826
  • sloc: ansic: 9,023; sh: 339; makefile: 208
file content (109 lines) | stat: -rw-r--r-- 1,993 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright  1998-2000 Harold Tay.
 * Copyright  2000- Patrik Rdman.
 */

#include <stdarg.h>
#include <unistd.h>
#include <readln.h>
#include <format.h>
#include <out.h>
#include <tokensep.h>

extern int debug;
static const char rcsid[] = "$Id$";
char *args[20];
char args_outbuf[1024];
char args_inbuf[512];

int args_write (int fd, char *fmt, ...)
{
   va_list ap;
   int len;

   va_start(ap, fmt);

   len = formatv(args_outbuf, sizeof (args_outbuf) - 2, fmt, ap);
   if (-1 == write(fd, args_outbuf, len))
   {
      *args_outbuf = '\0';
      return (-1);
   }
   args_outbuf[len - 2] = '\0';
   if (debug >= 2)
      log("-> %s", args_outbuf);
   return (len);
}

int args_read (struct readln *rp)
{
   char *p;
   int len;
   int i;

   len = readln(rp, &p, '\n');
   if (len <= 0)
   {
      *args_inbuf = '\0';
      return (-1);
   }

   for (len--; len > 0; len--)
   {
      switch (p[len])
      {
         case '\n':
         case '\r':
            continue;
      }
      break;
   }
   p[len + 1] = '\0';

   for (i = 0; i < sizeof (args_inbuf) - 1 && i <= len; i++)
      args_inbuf[i] = p[i];
   args_inbuf[i] = '\0';
   if (debug >= 2)
      log("<- %s", args_inbuf);
   if (len >= 512)
      return (0);

   for (i = 0; i < 20; i++)
      if (!(args[i] = tokensep(&p, " \t\r\n")))
         break;
   return (i);
}

void args_report (char *tag)
{
   log("%sSent \"%s\" got \"%s\"", tag ? tag : "", args_outbuf, args_inbuf);
}

void args_flushtodot (struct readln *rp)
{
   int tmo;

   tmo = rp->tmo;
   rp->tmo = 20;
   while (1)
   {
      int len;
      char *p;

      len = readln(rp, &p, '\n');
      if (len <= 0)
         break;
      if (len > 1 && '.' == *p)
      {
         if ('\r' == p[len - 2])
            p[len - 2] = '\0';
         else
            p[len - 1] = '\0';
         if (!p[1])
            break;
      }
   }
   rp->tmo = tmo;
}