File: util.cc

package info (click to toggle)
freetalk 3.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,744 kB
  • ctags: 532
  • sloc: sh: 4,387; cpp: 3,284; lisp: 1,039; makefile: 128; sed: 16
file content (151 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
  Copyright (c) 2005, 2006, 2007 Freetalk Core Team
  This file is part of Freetalk.
  
  Freetalk is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3 of the License,
  or (at your option) any later version.
  
  Freetalk is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see
  <http://www.gnu.org/licenses/>.
*/

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#include "util.h"
#include "freetalk.h"

/*
 * Parse a string containing a JID into its component parts and put them into
 * the jid_t.
 *
 * Ref. Section 3.1, RFC 3920
 */

void
parse_jid_string (char *s, jid_t *jid)
{
  char *str = strdup (s);
  char *at = strchr (str, '@');
  char *domain = str;
  char *slash;
     
  if (at)
    {
      *at = '\0';
      jid->node = strdup (str);
      domain = at+1;
    }

  slash = strchr (domain, '/');
     
  if (slash)
    {
      *slash = '\0';
      jid->domain = strdup (domain);
      jid->resource = strdup (slash+1);
    }
  else
    {
      jid->domain = strdup (domain);
      jid->resource = strdup ("GNU Freetalk");   /* Loudmouth complains if this is NULL */
    }

  free (str);
}

char *
first_word (char *full_line)
{
  while (full_line && (*full_line == ' '))
    full_line ++;
  return full_line;
}

char *
second_word (char *full_line)
{
  if (!full_line)
    return NULL;


  while (full_line &&
	 ( *full_line == ' ' ||
	   *full_line == '\t' ||
	   *full_line == '\r' ||
	   *full_line == '\n'))
    full_line ++;

  if (!full_line)
    return NULL;

  while (full_line && 
	 *full_line != ' ' && 
	 *full_line != '\t' && 
	 *full_line != '\r' && 
	 *full_line != '\n')
    full_line ++;

  if (!full_line)
    return NULL;

  while (full_line &&
	 ( *full_line == ' ' ||
	   *full_line == '\t' ||
	   *full_line == '\r' ||
	   *full_line == '\n'))
    full_line ++;

  return *full_line ? full_line : NULL;
}


void 
async_printf (const char *fmt, va_list ap)      
{
  int tmp_rl_point = rl_point;        
  int n = rl_end;
  unsigned int i;

  if (rl_end >= 0 ) {                 
    rl_kill_text (0, rl_end);
    rl_redisplay ();                  
  }                                   
  printf ("\r");
  for (i=0 ; i<=strlen (state.prompt) ; i++)
    printf (" ");
  printf ("\r");
  vprintf (fmt, ap);            
  printf ("\n");                      
  fflush(stdout);                     
  if (n) {                 
    rl_do_undo ();                  
    rl_point = tmp_rl_point;          
    rl_reset_line_state ();           
  }                                   
  rl_forced_update_display ();        
}

void 
sync_printf (const char *fmt, va_list ap)
{
  vprintf (fmt, ap);
  printf ("\n");
  fflush (stdout);
}

void
check_first_run (void)
{
  if( system ("sh " DATADIR "/" PACKAGE_NAME "/extensions/first-time-run.sh") >> 8 )
    exit (1);
}