~ubuntu-branches/ubuntu/vivid/atlas/vivid

« back to all changes in this revision

Viewing changes to CONFIG/include/atlas_sys.h

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-06-11 15:58:16 UTC
  • mfrom: (1.1.3 upstream)
  • mto: (2.2.21 experimental)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20130611155816-b72z8f621tuhbzn0
Tags: upstream-3.10.1
Import upstream version 3.10.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef ATLAS_SYS_H
 
2
   #define ATLAS_SYS_H
 
3
/*
 
4
 * This file contains routines to interact with the system (as in the C
 
5
 * `system' command), and related I/O
 
6
 */
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
#include <ctype.h>
 
11
#include <assert.h>
 
12
 
 
13
static char *NewStringCopy(char *old)
 
14
/*
 
15
 * RETURNS: newly allocates string containing copy of string old
 
16
 * NOTE: old is not modified.
 
17
 */
 
18
{
 
19
   char *new;
 
20
   if (!old)
 
21
      return(NULL);
 
22
   new = malloc(sizeof(char)*(strlen(old)+1));
 
23
   assert(new);
 
24
   strcpy(new, old);
 
25
   return(new);
 
26
}
 
27
 
 
28
static char *NewAppendedString0(char *old, char *app)
 
29
/*
 
30
 * RETURNS: string holding : old + app
 
31
 * NOTE: frees old string after copy
 
32
 */
 
33
{
 
34
   char *new;
 
35
   if (!old)
 
36
   {
 
37
      new = malloc(sizeof(char)*(strlen(app)+1));
 
38
      assert(new);
 
39
      strcpy(new, app);
 
40
   }
 
41
   else
 
42
   {
 
43
      new = malloc(sizeof(char)*(strlen(old) + strlen(app)+1));
 
44
      assert(new);
 
45
      strcpy(new, old);
 
46
      strcat(new, app);
 
47
      free(old);
 
48
   }
 
49
   return(new);
 
50
}
 
51
 
 
52
static char *NewAppendedString(char *old, char *app)
 
53
/*
 
54
 * RETURNS: string holding : old + " " + app
 
55
 * NOTE: frees old string after copy
 
56
 */
 
57
 
 
58
{
 
59
   char *new;
 
60
   if (!old)
 
61
   {
 
62
      new = malloc(sizeof(char)*(strlen(app)+1));
 
63
      assert(new);
 
64
      strcpy(new, app);
 
65
   }
 
66
   else
 
67
   {
 
68
      new = malloc(sizeof(char)*(strlen(old) + strlen(app)+2));
 
69
      assert(new);
 
70
      strcpy(new, old);
 
71
      strcat(new, " ");
 
72
      strcat(new, app);
 
73
      free(old);
 
74
   }
 
75
   return(new);
 
76
}
 
77
 
 
78
static char *NewAppendedStrings(char *old, char *app0, char *app1)
 
79
/*
 
80
 * RETURNS: string holding : old + " " + app0 + " " + app1
 
81
 * NOTE: frees old string after copy
 
82
 */
 
83
 
 
84
{
 
85
   char *new;
 
86
   int len;
 
87
 
 
88
   assert(app0 && app1);
 
89
   len = strlen(app0) + strlen(app1) + 2;
 
90
   if (!old)
 
91
   {
 
92
      new = malloc(sizeof(char)*len);
 
93
      assert(new);
 
94
      sprintf(new, "%s %s", app0, app1);
 
95
   }
 
96
   else
 
97
   {
 
98
      len += strlen(old) + 1;
 
99
      new = malloc(sizeof(char)*len);
 
100
      assert(new);
 
101
      sprintf(new, "%s %s %s", old, app0, app1);
 
102
      free(old);
 
103
   }
 
104
   return(new);
 
105
}
 
106
 
 
107
static char *ATL_fgets(char *sout, int *plen, FILE *fpin)
 
108
/*
 
109
 * This routine returns a pointer to a single line of of file fpin.
 
110
 * If the plen-length string sout is long enough to hold the file's line,
 
111
 * then sout will be the return value.  Otherwise sout will be freed and
 
112
 * a new string will be returned.
 
113
 * Upon EOF/error: sout is de-allocated, *len=0, & NULL is returned;
 
114
 * *len is the length of sout on input, and of the returned string on output.
 
115
 */
 
116
{
 
117
   int len = *plen;
 
118
   if (!sout || len < 1)
 
119
   {
 
120
      *plen = len = 128;
 
121
      sout = malloc(len*sizeof(char));
 
122
      assert(sout);
 
123
   }
 
124
/*
 
125
 * See if there is a line left in file
 
126
 */
 
127
   if (fgets(sout, len, fpin))
 
128
   {
 
129
      int i;
 
130
      for (i=0; sout[i]; i++);
 
131
      assert(i > 0);
 
132
      if (sout[i-1] == '\n')    /* if this is complete line */
 
133
         return(sout);          /* we are done, return it */
 
134
/*
 
135
 *    Continue doubling string length until we can fit the whole string
 
136
 */
 
137
      while (sout[i-1] != '\n')
 
138
      {
 
139
         char *sp;
 
140
         int len0 = len;
 
141
 
 
142
         *plen = (len += len);
 
143
         sp = malloc(len*sizeof(char));
 
144
         assert(sp);
 
145
         strcpy(sp, sout);
 
146
         free(sout);
 
147
         sout = sp;
 
148
         sp += i;
 
149
         if (!fgets(sp, len0, fpin))
 
150
            return(sout);
 
151
         for (; sout[i]; i++);
 
152
      }
 
153
      return(sout);
 
154
 
 
155
   }
 
156
   else
 
157
   {
 
158
      *plen = 0;
 
159
      free(sout);
 
160
   }
 
161
   return(NULL);
 
162
}
 
163
 
 
164
static char *ATL_fgets_CWS(char *sout, int *plen, FILE *fpin)
 
165
/*
 
166
 * This routine returns a pointer to a single line of of file fpin.
 
167
 * It then compresses the whitespace in the line for ease of parsing:
 
168
 * (1) The first character in the line is non-whitespace
 
169
 * (2) The last character in the line is non-whitespace
 
170
 * (3) Any whitespace string of 1 or more ws chars is replaced with 1 ' '
 
171
 * (4) If the entire line is whitespace, get another until EOF or non-ws
 
172
 * If the size-len string sout is long enough to hold the file's line,
 
173
 * then sout will be the return value.  Otherwise sout will be freed and
 
174
 * a new string will be returned.
 
175
 * Upon EOF/error: sout is de-allocated, *len=0, & NULL is returned;
 
176
 * *len is the length of sout in input, and of the returned string on output.
 
177
 */
 
178
{
 
179
   int i, j;
 
180
   char *sp;
 
181
/*
 
182
 * Find the end of any preceding whitespace line; if the whole line is
 
183
 * whitespace, keep getting lines until we've got one with some non-ws chars
 
184
 */
 
185
   do
 
186
   {
 
187
      sout = ATL_fgets(sout, plen, fpin);
 
188
      if (!sout)
 
189
         return(NULL);
 
190
      for (i=0; isspace(sout[i]); i++);
 
191
   }
 
192
   while (sout[i] == '\0');
 
193
/*
 
194
 * Now, go through line, replacing all whitespace with single ' '
 
195
 */
 
196
   for (sp=sout+i,j=0; sp[j]; j++)
 
197
   {
 
198
      if (isspace(sp[j]))
 
199
      {
 
200
         sout[j] = ' ';
 
201
         while (isspace(sp[j])) sp++;
 
202
         sp--;
 
203
      }
 
204
      else
 
205
         sout[j] = sp[j];
 
206
   }
 
207
/*
 
208
 * Shave off any trailing ws (can only be one due to above)
 
209
 */
 
210
   if (isspace(sout[j-1]))
 
211
      sout[j-1] = '\0';
 
212
   else
 
213
      sout[j] = '\0';
 
214
   return(sout);
 
215
}
 
216
 
 
217
static char *ATL_tmpnam(void)
 
218
{
 
219
   static char tnam[L_tmpnam];
 
220
   static char FirstTime=1;
 
221
   if (FirstTime)
 
222
   {
 
223
      FirstTime = 0;
 
224
      assert(tmpnam(tnam));
 
225
   }
 
226
   return(tnam);
 
227
}
 
228
 
 
229
static FILE *atlsys(char *targ, char *cmnd, int verb, int IgnoreErr)
 
230
/*
 
231
 * Executes command cmnd, returns open ("r" mode) file stream to output of
 
232
 * command.  If IgnoreErr is 0, then return NULL on error.
 
233
 */
 
234
{
 
235
   char *tnam, *sp;
 
236
   int i;
 
237
   FILE *output=NULL;
 
238
 
 
239
   tnam = ATL_tmpnam();
 
240
   if (targ)
 
241
   {
 
242
      i = strlen(targ) + strlen(cmnd) + strlen(tnam) + 24;
 
243
      sp = malloc(i*sizeof(char));
 
244
      assert(sp);
 
245
      sprintf(sp, "ssh %s \"%s\" > %s 2>&1 \n", targ, cmnd, tnam);
 
246
   }
 
247
   else
 
248
   {
 
249
      i = strlen(cmnd) + strlen(tnam) + 16;
 
250
      sp = malloc(i*sizeof(char));
 
251
      assert(sp);
 
252
      sprintf(sp, "%s > %s 2>&1\n", cmnd, tnam);
 
253
   }
 
254
   i = system(sp);
 
255
   if (i && verb)
 
256
   {
 
257
      fprintf(stderr, "\nierr=%d in command='%s'!\n\n", i, cmnd);
 
258
      if (verb > 1)
 
259
      {
 
260
         fprintf(stderr, "OUTPUT:\n=======\n");
 
261
         sprintf(sp, "cat %s", tnam);
 
262
         system(sp);
 
263
      }
 
264
   }
 
265
   free(sp);
 
266
   if (!i || IgnoreErr)
 
267
      output = fopen(tnam, "r");
 
268
   return(output);
 
269
}
 
270
 
 
271
static char *atlsys_1L(char *targ, char *cmnd, int verb, int CWS)
 
272
/*
 
273
 * Executes system(cmnd), returns 1st line as allocated string.  Returns NULL
 
274
 * on error.
 
275
 */
 
276
{
 
277
   FILE *fp;
 
278
   char *ln=NULL;
 
279
   int len=0;
 
280
 
 
281
   fp = atlsys(targ, cmnd, verb, 0);
 
282
   if (fp)
 
283
   {
 
284
      if (CWS)
 
285
         ln = ATL_fgets_CWS(ln, &len, fp);
 
286
      else
 
287
         ln = ATL_fgets(ln, &len, fp);
 
288
      fclose(fp);
 
289
   }
 
290
   return(ln);
 
291
}
 
292
#endif