changed long to int (64 bit system's compatibilty)
[lcr.git] / genrc.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** generate start/stop script                                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include "macro.h"
17
18 char mode[256];
19 int type[256];
20 int ptp[256];
21 int port[256];
22 char name[256];
23 int coredebug=0, carddebug=0, l1debug=0, l2debug=0, l3debug=0, dspdebug=0;
24 int lawopt=0;
25
26 static struct cards {
27         char *card;
28         char *module;
29         int ntmode;
30         int isac;
31         int ports;
32 } cards[] = {
33 //      { "AVM Fritz PCI (PNP)", "avmfritz", 0, 1, 1},
34         { "HFC PCI (Cologne Chip)", "hfcpci", 1, 0, 1},
35         { "HFC-4S 4 S/T Ports (Cologne Chip)", "hfcmulti", 1, 0, 4},
36         { "HFC-8S 8 S/T Ports (Cologne Chip)", "hfcmulti", 1, 0, 8},
37         { "HFC-E1 1 E1 Port (Cologne Chip)", "hfcmulti", 1, 0, 1},
38         { "HFC-S USB (Cologne Chip)", "hfcsusb", 1, 0, 1},
39 //      { "HFC-S MINI (Cologne Chip)", "hfcsmini", 1, 0, 1},
40 //      { "XHFC (Cologne Chip)", "xhfc", 1, 0, 1},
41 //      { "Sedlbaur FAX", "sedlfax", 0, 1, 1},
42 //      { "Winbond 6692 PCI", "w6692pci", 0, 0, 1},
43         { NULL, NULL, 0, 0},
44 };
45
46 int main(void)
47 {
48         FILE *fp;
49         int i = 0, j, jj, n, anyte = 0, remove_isac;
50         char input[256];
51         char protocol[1024], layermask[1024], types[256];
52
53         printf("This program is outdated and requires update to mISDN V2 API\n");
54         return (0);
55
56         printf("\n\nThis program generates a script, which is used to start/stop/restart mISDN\n");
57         printf("driver. All configuration of cards is done for using with the LCR.\n");
58
59         while(i < (int)sizeof(mode)) /* number of cards */
60         {
61                 moreports:
62                 do
63                 {
64                         printf("\nEnter mode of isdn port #%d. Should it run in NT-mode (for internal\nphones), or in TE-mode (for external lines)? If you do not like to add more\ncards, say 'done'.\n[nt | te | done]: ", i+1); fflush(stdout);
65                         scanf("%s", input);
66                 } while (input[0]!='t' && input[0]!='n' && input[0]!='d');
67                 mode[i] = input[0];
68                 if (mode[i] == 'd')
69                         break;
70                 ptp[i] = 0;
71                 do
72                 {
73                         printf("\nIs your port #%d connected to point-to-multipoint line/phone, which supports multiple\ntelephones (Mehrgeräteanschluss) OR is it a point-to-point link which is used\nfor LCR and supports extension dialing (Anlagenanschluss)?\n[ptp | ptm]: ", i+1); fflush(stdout);
74                         scanf("%s", input);
75                 } while (!!strcmp(input,"ptp") && !!strcmp(input,"ptm"));
76                 ptp[i] = (input[2]=='p')?1:0;
77                 anyte = 1;
78
79                 if (!i)
80                 {
81                         askcard:
82                         do
83                         {
84                                 printf("\nSelect driver of ISDN port #%d.\n\n", i+1);
85                                 jj = 0;
86                                 while(cards[jj].card)
87                                 {
88                                         if (cards[jj].ntmode || mode[i]!='n')
89                                                 printf(" (%d) %s\n", jj+1, cards[jj].card);
90                                         jj++;
91                                 }
92                                 printf("\n%sSelect card number[1-n]: ", (mode[i]=='n')?"Your card will run in NT-mode. The shown cards are capable of providing\nhardware layer for NT-mode.\n":""); fflush(stdout);
93                                 scanf("%s", input);
94                         } while (atoi(input) <= 0);
95                         type[i] = atoi(input);
96                         port[i] = 1;
97                         j = 0;
98                         while(j < jj)
99                         {
100                                 if (j+1==type[i] && (cards[j].ntmode || mode[i]!='n'))
101                                         break;
102                                 j++;
103                         }
104                         if (j == jj)
105                         {
106                                 printf("\n\nWrong selection, please try again.\n");
107                                 goto askcard;
108                         }
109                 } else
110                 if (cards[type[i-1]-1].ports == port[i-1])
111                         goto askcard;
112                 else {
113                         type[i] = type[i-1];
114                         port[i] = port[i-1]+1;
115                         printf("\nUsing port %d of card '%s'.", port[i], cards[type[i]-1].card);
116                 }
117
118                 printf("\n\n\nSummary: Port #%d of type %s will run in %s-mode and %s-mode.\n", i+1, cards[type[i]-1].card, (mode[i]=='n')?"NT":"TE", (ptp[i])?"point-to-point":"point-to-multipoint");
119
120                 i++;
121         }
122
123         if (!i)
124         {
125                 printf("\nNo ports/cards defined!\n");
126                 return(-1);
127         }
128         if (cards[type[i-1]-1].ports > port[i-1])
129         {
130                 printf("\nNot all ports for the last card are defined. Please do that even if they will be not\nused! Select 'NT-mode' for these unused ports.\n");
131                 goto moreports;
132         }
133
134         printf("\nEnter LAW audio mode. For a-LAW (default), just enter 0. For u-LAW enter 1.\n[0..n | 0xn]: "); fflush(stdout);
135         scanf("%s", input);
136         lawopt = strtoul(input, NULL, 0);
137         printf("\nEnter debugging flags of mISDN core. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
138         scanf("%s", input);
139         coredebug = strtoul(input, NULL, 0);
140         printf("\nEnter debugging flags of cards. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
141         scanf("%s", input);
142         carddebug = strtoul(input, NULL, 0);
143         printf("\nEnter l1 debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
144         scanf("%s", input);
145         l1debug = strtoul(input, NULL, 0);
146         printf("\nEnter l2 debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
147         scanf("%s", input);
148         l2debug = strtoul(input, NULL, 0);
149         printf("\nEnter l3 debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
150         scanf("%s", input);
151         l3debug = strtoul(input, NULL, 0);
152         printf("\nEnter dsp debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
153         scanf("%s", input);
154         dspdebug = strtoul(input, NULL, 0);
155
156         n = i;
157
158         printf("\nWhere do you like to load the modules from, enter 0 for default, 1 for\n'/usr/local/lcr/modules/' or the full path.\n[0 | 1 | <path>]: "); fflush(stdout);
159         scanf("%s", input);
160         if (!strcmp(input, "0"))
161                 SCPY(input, "");
162         if (!strcmp(input, "1"))
163                 SCPY(input, "/usr/local/lcr/modules");
164         if (input[0]) if (input[strlen(input)-1] != '/')
165                 SCAT(input, "/");
166
167         printf("\n\nFinally tell me where to write the mISDN rc file.\Enter the name 'mISDN' for current directory.\nYou may want to say '/usr/local/lcr/mISDN' or '/etc/rc.d/mISDN'\n: "); fflush(stdout);
168         scanf("%s", name);
169         if (!(fp=fopen(name, "w")))
170         {
171                 fprintf(stderr, "\nError: Failed to open '%s', try again.\n", name);
172                 exit(EXIT_FAILURE);
173         }
174         fprintf(fp, "# rc script for mISDN driver\n\n");
175         fprintf(fp, "case \"$1\" in\n");
176         fprintf(fp, "\tstart|--start)\n");
177         fprintf(fp, "\t\t%s %smISDN_core%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", coredebug);
178         if (anyte)
179         {
180                 fprintf(fp, "\t\t%s %smISDN_l1%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", l1debug);
181                 fprintf(fp, "\t\t%s %smISDN_l2%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", l2debug);
182                 fprintf(fp, "\t\t%s %sl3udss1%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", l3debug);
183         }
184         fprintf(fp, "\t\t%s %smISDN_dsp%s debug=0x%x options=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", dspdebug, lawopt);
185         j = 0;
186         while(cards[j].card)
187         {
188                 protocol[0] = layermask[0] = types[0] = '\0';
189                 i = 0;
190                 while(i < n)
191                 {
192                         if (j+1 == type[i])
193                         {
194                                 if (!strcmp(cards[j].module, "hfcmulti") && port[i]==1)
195                                         UPRINT(strchr(types,'\0'), "0x%x,", cards[j].ports+((lawopt)?0x100:0)+0x200);
196                                 UPRINT(strchr(protocol,'\0'), "0x%x,", ((mode[i]=='n')?0x12:0x2) + (ptp[i]?0x20:0x0));
197                                 UPRINT(strchr(layermask,'\0'), "0x%x,", (mode[i]=='n')?0x3:0x0f);
198                         }
199                         i++;
200                 }
201                 if (protocol[0])
202                 {
203                         protocol[strlen(protocol)-1] = '\0';
204                         layermask[strlen(layermask)-1] = '\0';
205                         if (types[0])
206                         {
207                                 types[strlen(types)-1] = '\0';
208                                 fprintf(fp, "\t\t%s %s%s%s type=%s protocol=%s layermask=%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, cards[j].module, input[0]?".ko":"", types, protocol, layermask, carddebug);
209                         } else
210                                 fprintf(fp, "\t\t%s %s%s%s protocol=%s layermask=%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, cards[j].module, input[0]?".ko":"", protocol, layermask, carddebug);
211                 }
212                 j++;
213         }
214         fprintf(fp, "\t\tsleep 1\n");
215         fprintf(fp, "\t\t;;\n\n");
216         fprintf(fp, "\tstop|--stop)\n");
217         remove_isac = 0;
218         j = 0;
219         while(cards[j].card)
220         {
221                 protocol[0] = 0;
222                 i = 0;
223                 while(i < n)
224                 {
225                         if (j+1 == type[i])
226                         {
227                                 protocol[0] = 1;
228                         }
229                         i++;
230                 }
231                 if (protocol[0])
232                 {
233                         fprintf(fp, "\t\trmmod %s\n", cards[j].module);
234                         if (cards[j].isac)
235                                 remove_isac = 1;
236                 }
237                 j++;
238         }
239         if (remove_isac)
240         {
241                 fprintf(fp, "\t\trmmod mISDN_isac\n");
242         }
243         fprintf(fp, "\t\trmmod mISDN_dsp\n");
244         if (anyte)
245         {
246                 fprintf(fp, "\t\trmmod l3udss1\n");
247                 fprintf(fp, "\t\trmmod mISDN_l2\n");
248                 fprintf(fp, "\t\trmmod mISDN_l1\n");
249         }
250         fprintf(fp, "\t\trmmod mISDN_core\n");
251         fprintf(fp, "\t\t;;\n\n");
252         fprintf(fp, "\trestart|--restart)\n");
253         fprintf(fp, "\t\tsh $0 stop\n");
254         fprintf(fp, "\t\tsleep 2 # some phones will release tei when layer 1 is down\n");
255         fprintf(fp, "\t\tsh $0 start\n");
256         fprintf(fp, "\t\t;;\n\n");
257         fprintf(fp, "\thelp|--help)\n");
258         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
259         fprintf(fp, "\t\texit 0\n");
260         fprintf(fp, "\t\t;;\n\n");
261         fprintf(fp, "\t*)\n");
262         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
263         fprintf(fp, "\t\texit 2\n");
264         fprintf(fp, "\t\t;;\n\n");
265         fprintf(fp, "esac\n");
266         fclose(fp);
267
268         printf("\nFile '%s' is written to the current directory.\n", name);
269 }
270
271