File: write_job_defaults.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (363 lines) | stat: -rw-r--r-- 9,828 bytes parent folder | download | duplicates (6)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "uti/sge_rmon.h"                       
#include "uti/sge_stdio.h"
#include "uti/sge_string.h"
#include "uti/sge_time.h"

#include "sgeobj/sge_ja_task.h"
#include "sgeobj/sge_answer.h"
#include "sgeobj/parse.h"
#include "sgeobj/sge_str.h"
#include "sgeobj/sge_id.h"

#include "parse_qsub.h"
#include "unparse_job_cull.h"
#include "write_job_defaults.h"
#include "symbols.h"
#include "msg_common.h"

static lList *write_defaults_file(lList *lp, char *filename, int flags);

/*
** NAME
**   write_job_defaults   -  defaults file from job structure
** PARAMETER
**   job       -  Job, JB_Type
**   filename  -  file to write defaults to
**   flags     -  FLG_FULL_CMDLINE or 0
**
** RETURN
**   answer list, AN_Type, or NULL if everything ok, 
**   the following stati can occur:
**   
** EXTERNAL
**   
** DESCRIPTION
**   unparses a job element into a default file
**   containing the option switches 
*/
lList *write_job_defaults(
sge_gdi_ctx_class_t *ctx,
lListElem *job,
char *filename,
int flags 
) {
   lList *alp;
   lListElem *aep;
   lList *cmdline = NULL;
   int status;
   
   DENTER(TOP_LAYER, "write_job_defaults");

   alp = cull_unparse_job_parameter(ctx, &cmdline, job, flags);
   for_each(aep, alp) {
      answer_exit_if_not_recoverable(aep);
      status = answer_get_status(aep); 
      if ((status != STATUS_OK) && (status != STATUS_EEXIST)) {
         goto do_exit;
      }
   }
   alp = write_defaults_file(cmdline, filename, flags);
   for_each(aep, alp) {
      answer_exit_if_not_recoverable(aep);
      status = answer_get_status(aep);
      if ((status != STATUS_OK) && (status != STATUS_EEXIST)) {
         goto do_exit;
      }
   }
   lFreeList(&alp);
do_exit:
   lFreeList(&cmdline);
   
   DRETURN(alp);
}

/*
** NAME
**   write_defaults_file   - writing job options
** PARAMETER
**   lp         - list of options, SPA_Type
**   filename   - name of file to write options to, 
**                or NULL, in this case stdout is used
**   flags      - FLG_FULL_CMDLINE or 0
**
** RETURN
**   answer list, AN_Type, or NULL if everything ok, 
**   the following stati can occur:
**   STATUS_EDISK     - error opening or writing to filename
**   STATUS_ESYNTAX   - the original command line string was
**                      not saved
**   
** EXTERNAL
**   
** DESCRIPTION
**   this function writes the given options to a file without
**   unparsing them. The following fields are written:
**   SPA_switch, then a blank (except for -l) and then
**   SPA_switch_arg, if the option had an argument on the commandline.
**   The switch -l is recognised separately, and no blank is
**   inserted between -l and the argument. This is a restriction to
**   the general usability of this function.
*/
static lList *write_defaults_file(
lList *lp,
char *filename,
int flags 
) {
   lListElem *ep;
   const char *cp;
   FILE *fp;
   lList *answer = NULL;
   int i = 0;
   char str[256 + 1];
   
   DENTER(BASIS_LAYER, "write_defaults_file");
   
   if (!filename) {
      fp = stdout;
   }
   else {
      fp = fopen(filename, "w");
      if (!fp) {
         sprintf(str, MSG_FILE_ERROROPENFILEXFORWRITING_S, filename);
         answer_list_add(&answer, str, STATUS_EDISK, ANSWER_QUALITY_ERROR);
         DRETURN(answer);
      }
   }
   for_each(ep, lp) {
      cp = lGetString(ep, SPA_switch);
      /*
      ** only real options are written to file
      ** except if the caller asks for full commandline
      */
      if (!cp) {
         continue;
      }
      if ((*cp != '-') && !(flags & FLG_FULL_CMDLINE)) {
         continue;
      }
      /*
      ** problem: exception for -l makes this function slightly
      ** less generally appliccable
      */
#if 0      
      if ((strlen(cp) > 1) && !strncmp(cp, "-l", 2)) {
         i = fprintf(fp, "%s", cp);
         i++;
      }
      else 
#endif      
      if (*cp == '-') {
         i = fprintf(fp, "%s ", cp);
      }
      if ((*cp == '-') && (i != (int) strlen(cp) + 1)) {
         sprintf(str, MSG_FILE_ERRORWRITETOFILEX_S, filename);
         answer_list_add(&answer, str, STATUS_EDISK, ANSWER_QUALITY_ERROR);
         if (filename) {
            FCLOSE(fp);
         }

         DRETURN(answer);
      }
      if (lGetUlong(ep, SPA_occurrence) & BIT_SPA_OCC_ARG) {
         cp = lGetString(ep, SPA_switch_arg);
         if (!cp) {
            sprintf(str, MSG_ANSWER_ARGUMENTMISSINGFORX_S , 
                    lGetString(ep, SPA_switch));
            answer_list_add(&answer, str, STATUS_ESYNTAX, ANSWER_QUALITY_ERROR);
            if (filename) {
               FCLOSE(fp);
            }   
               
            DRETURN(answer);
         }
         i = fprintf(fp, "%s ", cp);
         if (i != (int) strlen(cp) + 1) {
            sprintf(str, MSG_FILE_ERRORWRITETOFILEX_S, filename);
            answer_list_add(&answer, str, STATUS_EDISK, ANSWER_QUALITY_ERROR);
            if (filename) {
               FCLOSE(fp);
            }   

            DRETURN(answer);
         }
      }
   }

   fprintf(fp, "\n");

   if (filename) {
      FCLOSE(fp);
   }

   DRETURN(answer);

FCLOSE_ERROR:
   answer_list_add_sprintf(&answer, STATUS_EDISK, ANSWER_QUALITY_ERROR,
                           MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
   DRETURN(answer);
}


#ifdef TEST
#include "sgeobj/sge_all_listsL.h"

int main(
int argc,
char **argv,
char **envp 
) {
   lList *cmdline = NULL;
   lListElem *job = NULL;
   lList *alp;
   lListElem *aep;
   u_long32 status = STATUS_OK;
   int do_exit = 0;

   DENTER_MAIN(TOP_LAYER, "test_write_defaults");

#ifdef __SGE_COMPILE_WITH_GETTEXT__  
   /* init language output for gettext() , it will use the right language */
   sge_init_language_func((gettext_func_type)        gettext,
                         (setlocale_func_type)      setlocale,
                         (bindtextdomain_func_type) bindtextdomain,
                         (textdomain_func_type)     textdomain);
   sge_init_language(NULL,NULL); 
#endif /* __SGE_COMPILE_WITH_GETTEXT__  */


   lInit(nmv);
   sge_setup(QSUB);
   alp = cull_parse_cmdline(argv + 1, envp, &cmdline, FLG_USE_PSEUDOS);
   for_each(aep, alp) {
      answer_exit_if_not_recoverable(aep);
      status = answer_get_status(aep);
      if ((status != STATUS_OK) && (status != STATUS_EEXIST)) {
         fprintf(stderr, "%s", lGetString(aep, AN_text));
         do_exit = 1;
      }
      else {
         printf("%s\n", lGetString(aep, AN_text));
      }
   }
   lFreeList(alp);
   if (do_exit) {
      SGE_EXIT(status);
   }

   alp = cull_parse_job_parameter(cmdline, &job);
   for_each(aep, alp) {
      answer_exit_if_not_recoverable(aep);
      status = answer_get_status(aep);
      if (status != STATUS_OK) {
         fprintf(stderr, "%s", lGetString(aep, AN_text));
         do_exit = 1;
      }
      else {
         printf("%s\n", lGetString(aep, AN_text));
      }
   }
   lFreeList(alp);
   if (do_exit) {
      SGE_EXIT(status);
   }

   alp = write_job_defaults(job, "wdtest.dat", FLG_FULL_CMDLINE);
   for_each(aep, alp) {
      answer_exit_if_not_recoverable(aep);
      status = answer_get_status(aep);
      if (status != STATUS_OK) {
         fprintf(stderr, "%s", lGetString(aep, AN_text));
         do_exit = 1;
      }
      else {
         printf("%s\n", lGetString(aep, AN_text));
      }
   }
   lFreeList(alp);
   if (do_exit) {
      SGE_EXIT(status);
   }

   return 0;
}

#endif

#ifdef TEST
#include "sgeobj/sge_all_listsL.h"
int main(int argc, char **argv, char **envp);

int main(
int argc,
char **argv,
char **envp 
) {
   lList *answer;
   lList *cmdline;


#ifdef __SGE_COMPILE_WITH_GETTEXT__  
   /* init language output for gettext() , it will use the right language */
   install_language_func((gettext_func_type)        gettext,
                         (setlocale_func_type)      setlocale,
                         (bindtextdomain_func_type) bindtextdomain,
                         (textdomain_func_type)     textdomain);
   sge_lang_init(NULL,NULL);   
#endif /* __SGE_COMPILE_WITH_GETTEXT__  */


   lInit(nmv);

   answer = parse_script_file(*(argv + 1), "", &cmdline, envp, FLG_USE_NO_PSEUDOS);
   if (answer) {
      lDumpList(stdout, answer, 0);
      exit(1);
   }
   lDumpList(stdout, cmdline, 4);

   answer = write_defaults_file(cmdline, NULL, 0);
   if (answer) {
      lDumpList(stdout, answer, 0);
      exit(1);
   }
   return 0;
}

#endif