1 | /* |
---|
2 | * $Id: utils.cc,v 1.10 2000-12-05 15:26:59 obachman Exp $ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <string.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <stdarg.h> |
---|
9 | #include <ctype.h> |
---|
10 | #include <sys/types.h> |
---|
11 | #include <sys/stat.h> |
---|
12 | #include <fcntl.h> |
---|
13 | #include <unistd.h> |
---|
14 | |
---|
15 | int modlineno; /* lineno within module */ |
---|
16 | |
---|
17 | #include "modgen.h" |
---|
18 | |
---|
19 | void init_system_type(); |
---|
20 | |
---|
21 | /*========================================================================*/ |
---|
22 | int init_modgen( |
---|
23 | moddefv module_def, |
---|
24 | char *filename |
---|
25 | ) |
---|
26 | { |
---|
27 | char tmpfile[64]; |
---|
28 | char *p, *q; |
---|
29 | |
---|
30 | modlineno = 0; |
---|
31 | |
---|
32 | if(module_def == NULL) return -1; |
---|
33 | memset(module_def, '\0', sizeof(moddef)); |
---|
34 | |
---|
35 | if ( (q=strrchr(filename, '/')) == NULL) q = filename; |
---|
36 | else q++; |
---|
37 | |
---|
38 | module_def->filename = (char *)malloc(strlen(q)+1); |
---|
39 | if(module_def->filename != NULL ) { |
---|
40 | memset(module_def->filename, '\0', strlen(q)+1); |
---|
41 | memcpy(module_def->filename, q, strlen(q)); |
---|
42 | } |
---|
43 | strncpy(tmpfile, q, sizeof(tmpfile)); |
---|
44 | p=strrchr(tmpfile, '.'); |
---|
45 | if(p!=NULL) *p='\0'; |
---|
46 | module_def->name = (char *)malloc(strlen(tmpfile)+1); |
---|
47 | memset(module_def->name, '\0', strlen(tmpfile)+1); |
---|
48 | memcpy(module_def->name, tmpfile, strlen(tmpfile)); |
---|
49 | |
---|
50 | init_system_type(); |
---|
51 | |
---|
52 | return (create_tmpfile(module_def)); |
---|
53 | } |
---|
54 | |
---|
55 | /*========================================================================*/ |
---|
56 | int create_tmpfile( |
---|
57 | moddefv module_def, |
---|
58 | int which |
---|
59 | ) |
---|
60 | { |
---|
61 | char tmpfile[64]; |
---|
62 | FILE *fp; |
---|
63 | |
---|
64 | memset(tmpfile, '\0', sizeof(tmpfile)); |
---|
65 | snprintf(tmpfile, sizeof(tmpfile), "modgen.tmpXXXXXX"); |
---|
66 | mktemp(tmpfile); |
---|
67 | |
---|
68 | if(debug)printf("create_tmpfile '%s'\n", tmpfile ); |
---|
69 | |
---|
70 | if (close(creat(tmpfile, 0600)) < 0) { |
---|
71 | (void) unlink (tmpfile); /* Blow it away!! */ |
---|
72 | return -1; |
---|
73 | } else if ((fp = fopen(tmpfile, "a+")) == NULL) { |
---|
74 | (void) unlink (tmpfile); /* Blow it away!! */ |
---|
75 | return -1; |
---|
76 | } else { |
---|
77 | (void) unlink (tmpfile); /* delete now to avoid turds... */ |
---|
78 | } |
---|
79 | |
---|
80 | switch(which) { |
---|
81 | case 0: module_def->fmtfp = fp; break; |
---|
82 | case 1: module_def->fmtfp2 = fp; break; |
---|
83 | case 2: module_def->fmtfp3 = fp; break; |
---|
84 | default: |
---|
85 | fclose(fp); |
---|
86 | return -1; |
---|
87 | } |
---|
88 | |
---|
89 | return 0; |
---|
90 | } |
---|
91 | |
---|
92 | /*========================================================================*/ |
---|
93 | char *build_filename( |
---|
94 | moddefv module, |
---|
95 | char *text, |
---|
96 | int what |
---|
97 | ) |
---|
98 | { |
---|
99 | char* p = (char*) malloc(512*sizeof(char)); |
---|
100 | |
---|
101 | switch(what) |
---|
102 | { |
---|
103 | case 1: |
---|
104 | snprintf(p, sizeof(p), "%s/%s.cc", module->name, text); |
---|
105 | break; |
---|
106 | case 2: |
---|
107 | snprintf(p, sizeof(p), "%s/%s.h", module->name, text); |
---|
108 | break; |
---|
109 | case 3: |
---|
110 | snprintf(p, sizeof(p), "%s/%s.bin", module->name, text); |
---|
111 | break; |
---|
112 | default: |
---|
113 | snprintf(p, sizeof(p), "%s/%s", module->name, text); |
---|
114 | break; |
---|
115 | } |
---|
116 | |
---|
117 | return p; |
---|
118 | } |
---|
119 | |
---|
120 | /*========================================================================*/ |
---|
121 | int myyyerror( |
---|
122 | char *fmt, ... |
---|
123 | ) |
---|
124 | { |
---|
125 | va_list ap; |
---|
126 | va_start(ap, fmt); |
---|
127 | vprintf(fmt, ap); |
---|
128 | va_end(ap); |
---|
129 | return 2; |
---|
130 | } |
---|