~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/clients/common/write_job_defaults.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <errno.h>
 
36
 
 
37
#include "uti/sge_stdio.h"
 
38
 
 
39
#include "symbols.h"
 
40
#include "sge_ja_task.h"
 
41
#include "sge_answer.h"
 
42
#include "sge_string.h"
 
43
#include "sge_time.h"
 
44
#include "parse_qsubL.h"
 
45
#include "sge_str.h"
 
46
#include "sge_idL.h"
 
47
#include "sge_job_refL.h"
 
48
#include "sgermon.h"                       
 
49
#include "unparse_job_cull.h"
 
50
#include "write_job_defaults.h"
 
51
 
 
52
#include "msg_common.h"
 
53
 
 
54
static lList *write_defaults_file(lList *lp, char *filename, int flags);
 
55
 
 
56
/*
 
57
** NAME
 
58
**   write_job_defaults   -  defaults file from job structure
 
59
** PARAMETER
 
60
**   job       -  Job, JB_Type
 
61
**   filename  -  file to write defaults to
 
62
**   flags     -  FLG_FULL_CMDLINE or 0
 
63
**
 
64
** RETURN
 
65
**   answer list, AN_Type, or NULL if everything ok, 
 
66
**   the following stati can occur:
 
67
**   
 
68
** EXTERNAL
 
69
**   
 
70
** DESCRIPTION
 
71
**   unparses a job element into a default file
 
72
**   containing the option switches 
 
73
*/
 
74
lList *write_job_defaults(
 
75
sge_gdi_ctx_class_t *ctx,
 
76
lListElem *job,
 
77
char *filename,
 
78
int flags 
 
79
) {
 
80
   lList *alp;
 
81
   lListElem *aep;
 
82
   lList *cmdline = NULL;
 
83
   int do_exit = 0;
 
84
   int status;
 
85
   
 
86
   DENTER(TOP_LAYER, "write_job_defaults");
 
87
 
 
88
   alp = cull_unparse_job_parameter(ctx, &cmdline, job, flags);
 
89
   for_each(aep, alp) {
 
90
      answer_exit_if_not_recoverable(aep);
 
91
      status = answer_get_status(aep); 
 
92
      if ((status != STATUS_OK) && (status != STATUS_EEXIST)) {
 
93
         do_exit = 1;
 
94
      }
 
95
   }
 
96
   if (do_exit) {
 
97
      DRETURN(alp);
 
98
   }
 
99
   lFreeList(&alp);
 
100
 
 
101
   alp = write_defaults_file(cmdline, filename, flags);
 
102
   for_each(aep, alp) {
 
103
      answer_exit_if_not_recoverable(aep);
 
104
      status = answer_get_status(aep);
 
105
      if ((status != STATUS_OK) && (status != STATUS_EEXIST)) {
 
106
         do_exit = 1;
 
107
      }
 
108
   }
 
109
   if (do_exit) {
 
110
      DRETURN(alp);
 
111
   }
 
112
   lFreeList(&alp);
 
113
   
 
114
   DRETURN(NULL);
 
115
}
 
116
 
 
117
/*
 
118
** NAME
 
119
**   write_defaults_file   - writing job options
 
120
** PARAMETER
 
121
**   lp         - list of options, SPA_Type
 
122
**   filename   - name of file to write options to, 
 
123
**                or NULL, in this case stdout is used
 
124
**   flags      - FLG_FULL_CMDLINE or 0
 
125
**
 
126
** RETURN
 
127
**   answer list, AN_Type, or NULL if everything ok, 
 
128
**   the following stati can occur:
 
129
**   STATUS_EDISK     - error opening or writing to filename
 
130
**   STATUS_ESYNTAX   - the original command line string was
 
131
**                      not saved
 
132
**   
 
133
** EXTERNAL
 
134
**   
 
135
** DESCRIPTION
 
136
**   this function writes the given options to a file without
 
137
**   unparsing them. The following fields are written:
 
138
**   SPA_switch, then a blank (except for -l) and then
 
139
**   SPA_switch_arg, if the option had an argument on the commandline.
 
140
**   The switch -l is recognised separately, and no blank is
 
141
**   inserted between -l and the argument. This is a restriction to
 
142
**   the general usability of this function.
 
143
*/
 
144
static lList *write_defaults_file(
 
145
lList *lp,
 
146
char *filename,
 
147
int flags 
 
148
) {
 
149
   lListElem *ep;
 
150
   const char *cp;
 
151
   FILE *fp;
 
152
   lList *answer = NULL;
 
153
   int i = 0;
 
154
   char str[256 + 1];
 
155
   
 
156
   DENTER(BASIS_LAYER, "write_defaults_file");
 
157
   
 
158
   if (!filename) {
 
159
      fp = stdout;
 
160
   }
 
161
   else {
 
162
      fp = fopen(filename, "w");
 
163
      if (!fp) {
 
164
         sprintf(str, MSG_FILE_ERROROPENFILEXFORWRITING_S, filename);
 
165
         answer_list_add(&answer, str, STATUS_EDISK, ANSWER_QUALITY_ERROR);
 
166
         DRETURN(answer);
 
167
      }
 
168
   }
 
169
   for_each(ep, lp) {
 
170
      cp = lGetString(ep, SPA_switch);
 
171
      /*
 
172
      ** only real options are written to file
 
173
      ** except if the caller asks for full commandline
 
174
      */
 
175
      if (!cp) {
 
176
         continue;
 
177
      }
 
178
      if ((*cp != '-') && !(flags & FLG_FULL_CMDLINE)) {
 
179
         continue;
 
180
      }
 
181
      /*
 
182
      ** problem: exception for -l makes this function slightly
 
183
      ** less generally appliccable
 
184
      */
 
185
#if 0      
 
186
      if ((strlen(cp) > 1) && !strncmp(cp, "-l", 2)) {
 
187
         i = fprintf(fp, "%s", cp);
 
188
         i++;
 
189
      }
 
190
      else 
 
191
#endif      
 
192
      if (*cp == '-') {
 
193
         i = fprintf(fp, "%s ", cp);
 
194
      }
 
195
      if ((*cp == '-') && (i != (int) strlen(cp) + 1)) {
 
196
         sprintf(str, MSG_FILE_ERRORWRITETOFILEX_S, filename);
 
197
         answer_list_add(&answer, str, STATUS_EDISK, ANSWER_QUALITY_ERROR);
 
198
         if (filename) {
 
199
            FCLOSE(fp);
 
200
         }
 
201
 
 
202
         DRETURN(answer);
 
203
      }
 
204
      if (lGetUlong(ep, SPA_occurrence) & BIT_SPA_OCC_ARG) {
 
205
         cp = lGetString(ep, SPA_switch_arg);
 
206
         if (!cp) {
 
207
            sprintf(str, MSG_ANSWER_ARGUMENTMISSINGFORX_S , 
 
208
                    lGetString(ep, SPA_switch));
 
209
            answer_list_add(&answer, str, STATUS_ESYNTAX, ANSWER_QUALITY_ERROR);
 
210
            if (filename) {
 
211
               FCLOSE(fp);
 
212
            }   
 
213
               
 
214
            DRETURN(answer);
 
215
         }
 
216
         i = fprintf(fp, "%s ", cp);
 
217
         if (i != (int) strlen(cp) + 1) {
 
218
            sprintf(str, MSG_FILE_ERRORWRITETOFILEX_S, filename);
 
219
            answer_list_add(&answer, str, STATUS_EDISK, ANSWER_QUALITY_ERROR);
 
220
            if (filename) {
 
221
               FCLOSE(fp);
 
222
            }   
 
223
 
 
224
            DRETURN(answer);
 
225
         }
 
226
      }
 
227
   }
 
228
 
 
229
   fprintf(fp, "\n");
 
230
 
 
231
   if (filename) {
 
232
      FCLOSE(fp);
 
233
   }
 
234
 
 
235
   DRETURN(answer);
 
236
 
 
237
FCLOSE_ERROR:
 
238
   answer_list_add_sprintf(&answer, STATUS_EDISK, ANSWER_QUALITY_ERROR,
 
239
                           MSG_FILE_ERRORCLOSEINGXY_SS, filename, strerror(errno));
 
240
   DRETURN(answer);
 
241
}
 
242
 
 
243
 
 
244
#ifdef TEST
 
245
#include "sge_all_listsL.h"
 
246
int main(int argc, char **argv, char **envp);
 
247
 
 
248
int main(
 
249
int argc,
 
250
char **argv,
 
251
char **envp 
 
252
) {
 
253
   lList *cmdline = NULL;
 
254
   lListElem *job = NULL;
 
255
   lList *alp;
 
256
   lListElem *aep;
 
257
   u_long32 status = STATUS_OK;
 
258
   int do_exit = 0;
 
259
 
 
260
   DENTER_MAIN(TOP_LAYER, "test_write_defaults");
 
261
 
 
262
#ifdef __SGE_COMPILE_WITH_GETTEXT__  
 
263
   /* init language output for gettext() , it will use the right language */
 
264
   sge_init_language_func((gettext_func_type)        gettext,
 
265
                         (setlocale_func_type)      setlocale,
 
266
                         (bindtextdomain_func_type) bindtextdomain,
 
267
                         (textdomain_func_type)     textdomain);
 
268
   sge_init_language(NULL,NULL); 
 
269
#endif /* __SGE_COMPILE_WITH_GETTEXT__  */
 
270
 
 
271
 
 
272
   lInit(nmv);
 
273
   sge_setup(QSUB);
 
274
   alp = cull_parse_cmdline(argv + 1, envp, &cmdline, FLG_USE_PSEUDOS);
 
275
   for_each(aep, alp) {
 
276
      answer_exit_if_not_recoverable(aep);
 
277
      status = answer_get_status(aep);
 
278
      if ((status != STATUS_OK) && (status != STATUS_EEXIST)) {
 
279
         fprintf(stderr, "%s", lGetString(aep, AN_text));
 
280
         do_exit = 1;
 
281
      }
 
282
      else {
 
283
         printf("%s\n", lGetString(aep, AN_text));
 
284
      }
 
285
   }
 
286
   lFreeList(alp);
 
287
   if (do_exit) {
 
288
      SGE_EXIT(status);
 
289
   }
 
290
 
 
291
   alp = cull_parse_job_parameter(cmdline, &job);
 
292
   for_each(aep, alp) {
 
293
      answer_exit_if_not_recoverable(aep);
 
294
      status = answer_get_status(aep);
 
295
      if (status != STATUS_OK) {
 
296
         fprintf(stderr, "%s", lGetString(aep, AN_text));
 
297
         do_exit = 1;
 
298
      }
 
299
      else {
 
300
         printf("%s\n", lGetString(aep, AN_text));
 
301
      }
 
302
   }
 
303
   lFreeList(alp);
 
304
   if (do_exit) {
 
305
      SGE_EXIT(status);
 
306
   }
 
307
 
 
308
   alp = write_job_defaults(job, "wdtest.dat", FLG_FULL_CMDLINE);
 
309
   for_each(aep, alp) {
 
310
      answer_exit_if_not_recoverable(aep);
 
311
      status = answer_get_status(aep);
 
312
      if (status != STATUS_OK) {
 
313
         fprintf(stderr, "%s", lGetString(aep, AN_text));
 
314
         do_exit = 1;
 
315
      }
 
316
      else {
 
317
         printf("%s\n", lGetString(aep, AN_text));
 
318
      }
 
319
   }
 
320
   lFreeList(alp);
 
321
   if (do_exit) {
 
322
      SGE_EXIT(status);
 
323
   }
 
324
 
 
325
   return 0;
 
326
}
 
327
 
 
328
#endif
 
329
 
 
330
#ifdef TEST
 
331
#include "sge_all_listsL.h"
 
332
int main(int argc, char **argv, char **envp);
 
333
 
 
334
int main(
 
335
int argc,
 
336
char **argv,
 
337
char **envp 
 
338
) {
 
339
   lList *answer;
 
340
   lList *cmdline;
 
341
 
 
342
 
 
343
#ifdef __SGE_COMPILE_WITH_GETTEXT__  
 
344
   /* init language output for gettext() , it will use the right language */
 
345
   install_language_func((gettext_func_type)        gettext,
 
346
                         (setlocale_func_type)      setlocale,
 
347
                         (bindtextdomain_func_type) bindtextdomain,
 
348
                         (textdomain_func_type)     textdomain);
 
349
   sge_lang_init(NULL,NULL);   
 
350
#endif /* __SGE_COMPILE_WITH_GETTEXT__  */
 
351
 
 
352
 
 
353
   lInit(nmv);
 
354
 
 
355
   answer = parse_script_file(*(argv + 1), "", &cmdline, envp, FLG_USE_NO_PSEUDOS);
 
356
   if (answer) {
 
357
      lDumpList(stdout, answer, 0);
 
358
      exit(1);
 
359
   }
 
360
   lDumpList(stdout, cmdline, 4);
 
361
 
 
362
   answer = write_defaults_file(cmdline, NULL, 0);
 
363
   if (answer) {
 
364
      lDumpList(stdout, answer, 0);
 
365
      exit(1);
 
366
   }
 
367
   return 0;
 
368
}
 
369
 
 
370
#endif
 
371